I have a dataframe like:
column1 column2 a 32 b 175 b 165 a 80
and I want to update the value (multiplied by 10) of every row of column2 whose value in column1 == 'a'. The result is expected to be like:
column1 column2 a 320 b 175 b 165 a 800
CodePudding user response:
Very short with loc
:
df.loc[df['column1']=='a', 'column2'] *= 10
You get:
column1 column2
0 a 320
1 b 175
2 b 165
3 a 800
CodePudding user response:
Try np.where
df['column2'] = np.where(df['column1'].eq('a'), df['column2'].mul(10), df['column2'])
Or df.mask
df['column2'] = df['column2'].mask(df['column1'].eq('a'), df['column2'].mul(10))
column1 column2
0 a 320
1 b 175
2 b 165
3 a 800