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

Replacing column value under a Group in Pandas

Hi I have a dataframe as below, I need to replace the column value in each group based on the condition using pandas. Please help

Input Data:

Name    Thing   type    flag
Steve   Car     High    NULL
Steve   Car     Low     NULL
Steve   Bike    Low     NULL
Steve   Bike    Avg     X
Steve   Plane   High    NULL
Steve   Plane   Low     NULL

the condition will be :

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

1. for a given name and thing if type is Low,Avg update the flag as X

2. for a given name and thing if type is High,Avg update the flag as X

Expected output :

Name    Thing   type    flag
Steve   Car     High    NULL
Steve   Car     Low     NULL
Steve   Bike    Low     X
Steve   Bike    Avg     X
Steve   Plane   High    NULL
Steve   Plane   Low     NULL

Tried so far :

df['flag'] = df['flag'].mask((df['type'] == 'Low') | (df['type'] == 'Avg'), 'X').groupby(df(['name','thing'])).transform('any')

>Solution :

Code

g = df.groupby(['Name', 'Thing'])['type']
cond = g.transform(lambda x: (set(x) == {'Low', 'Avg'}) | (set(x) == {'High', 'Avg'}))
df.loc[cond, 'flag'] = 'X'

df:

    Name  Thing  type flag
0  Steve    Car  High  NaN
1  Steve    Car   Low  NaN
2  Steve   Bike   Low    X
3  Steve   Bike   Avg    X
4  Steve  Plane  High  NaN
5  Steve  Plane   Low  NaN
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