Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – Having trouble assigning value into new col based on data from another cell

I have data that looks like;

ID    File
1     this_file_whatever.ext1
2     this_whatever.ext2
3     this_is_ok_pooh.ext3

I am trying to get the extension and put the key from a dict in a new col based on the extension in File.

    def create_filegroups(row):
    filegroup_dict = {
        'GroupA': 'ext1',
        'GroupB': 'ext2',
        'GroupC': 'ext3'
    }
    if '.' in row['Name']:
        test = row['Name'].split(".",1)[1]
    return test

DF = build_df()
DF['COL3'] = DF.apply(create_filegroups(row), axis=1)
print(DF)

I can’t figure out what I am doing wrong. The dict compare I can do when I get there, but I can’t seem to apply a function to the cells.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I believe you need pandas.Series.map after extracting the file extension from the column File.

Try this:

df['COL3']= (
                df['File']
                    .str.extract(r'\w+\.(\w+)', expand=False)
                    .map({k:v for v,k in filegroup_dict.items()})
            )

# Output :

print(df)

   ID                     File    COL3
0   1  this_file_whatever.ext1  GroupA
1   2       this_whatever.ext2  GroupB
2   3     this_is_ok_pooh.ext3  GroupC
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading