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

How to update value in pandas data frame based on the condition?

I need to replace specific values from the data frame based on the country level value.

df:

Country    Val1    Val2
Argentina   12      C
Argentina   23      34
Argentina   NaN     92
Argentina   -       29
Argentina   U       73
Germany     37      47
Germany     12      23
Germany     U      NaN
Germany     NaN     -
Germany     C       C

df_output:

Country    Val1    Val2
Argentina   12      92
Argentina   23      34
Argentina   NaN     92
Argentina   -       29
Argentina   23      73
Germany     37      47
Germany     12      23
Germany     37      NaN
Germany     NaN     -
Germany     37      47

I need to replace "C" and "U" Value with max Number of that column based on country.

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 :

Create index by Country column, then test C, U by DataFrame.isin and replace if match condition by DataFrame.mask by maximal values per groups – but before convert values to numeric:

df = df.set_index('Country')

m = df.isin(['C','U'])

df1 = (df.mask(m, df.apply(pd.to_numeric, errors='coerce')
                    .groupby('Country')
                    .transform('max'))
        .reset_index())

print (df1)
     Country  Val1  Val2
0  Argentina    12  92.0
1  Argentina    23    34
2  Argentina   NaN    92
3  Argentina     -    29
4  Argentina  23.0    73
5    Germany    37    47
6    Germany    12    23
7    Germany  37.0   NaN
8    Germany   NaN     -
9    Germany  37.0  47.0
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