[英]Pandas update values of multiple rows of a column based on values of another column
I have a dataframe like:我有一个 dataframe 像:
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'.我想更新 column2 的每一行的值(乘以 10),其 column1 == 'a' 中的值。 The result is expected to be like:
预计结果如下:
column1 column2 a 320 b 175 b 165 a 800
Very short with loc
:很短
loc
:
df.loc[df['column1']=='a', 'column2'] *= 10
You get:你得到:
column1 column2
0 a 320
1 b 175
2 b 165
3 a 800
Try np.where
试试
np.where
df['column2'] = np.where(df['column1'].eq('a'), df['column2'].mul(10), df['column2'])
Or df.mask
或者
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.