简体   繁体   English

Pandas 根据其他列值更改值

[英]Pandas change value based on other column values

I want to change the value of each item in the column 'ageatbirth' to '1' if the 'race' is 2. In pseudocode:如果'race'是2,我想将'ageatbirth'列中每个项目的值更改为'1'。在伪代码中:

If 'race' == 2 and 'ageatbirth' == 2:

'ageatbirth' == 1 

Is there an easy way to do this for a very large dataset?对于非常大的数据集,是否有一种简单的方法可以做到这一点?

Use利用

m = df['race'].eq(2) & df['ageatbirth'].eq(2)

df['ageatbirth'] = df['ageatbirth'].mask(m, 1)
# or
df.loc[m, 'ageatbirth'] = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM