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: Iterate over values in a series and replace with dictionary values when key matches series value

I’m trying to iterate over a column in a dataframe and when the value matches a key from my dictionary it should then replace the value in another column with the value of the matching key.

    df = pd.DataFrame({'id': ['123', '456', '789'], 'Full': ['Yes', 'No', 'Yes'], 'Cat':['','','']})
    cats = {'123':'A', '456':'B', '789':'C'}
    for val in df.id:
        for key, cat in cats.items():
            if key == val:
                df.Cat.loc[(df.Full == 'Yes')] = cat
    df
        id  Full Cat
    0   123 Yes  C
    1   456 No  
    2   789 Yes  C

I would expect id 123 to have a Cat of ‘A’ but instead it only returns ‘C’

Can anyone explain to me why the it isn’t iterating over the keys in dictionary?

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 :

You can use Series.replace and pass the dictionary, and assign the result to Cat column:

>>> df['Cat'] = df.id.replace(cats)
#output:

    id Full Cat
0  123  Yes   A
1  456   No   B
2  789  Yes   C

Or, if you intend to replace in only the rows with Full as Yes one way is to simply apply a function on axis=1 then implement the logic for each rows:

>>> df['Cat'] = df.apply(lambda x: cats.get(x.id, '') if x.Full == 'Yes' else '',
                     axis=1)

    id Full Cat
0  123  Yes   A
1  456   No    
2  789  Yes   C
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