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?
CodePudding user response:
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 False
s 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