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

Mutate pandas dataframe cell values

I want to mutate column c at a specific row by adding that row and another row.

df = pd.DataFrame({
    'A': [0,1,2,3],
    'B': [0,1,2,3],
    'C': [10,10,10,10]
})

mask1 = df['A']==1
mask2 = df['B']==2
df.loc[mask1, 'C'] = df.loc[mask1, 'C'] + df.loc[mask2, 'C']

In the last line, because we are adding two pd.Series together, it tries to match the index and therefore would return NaN instead of the expected 10 + 10=20.

How do I do this properly?

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 :

IIUC use select first value from Series by df.loc[mask2, 'C']

df.loc[mask1, 'C'] = df.loc[mask1, 'C'] + df.loc[mask2, 'C'].iat[0]

df.loc[mask1, 'C'] = df.loc[mask1, 'C'] + df.loc[mask2, 'C'].to_numpy()[0]

If possible mask2 return all Falses use:

df.loc[mask1, 'C'] = df.loc[mask1, 'C'] + next(iter(df.loc[mask2, 'C']), 0)
print (df)
   A  B   C
0  0  0  10
1  1  1  20
2  2  2  10
3  3  3  10
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