如何更新DataFram中的特定值

2024-09-27 18:09:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是Python中的熊猫

如何将“EstimatedSalary”设置为零,从而将21000以下的数据帧的所有值设置为零?意思是我希望前两行有0,而不是19000和20000

ID          Gender  Age EstimatedSalary Purchased
15624510    Male    19  19000           0
15810944    Male    35  20000           0
15668575    Female  26  43000           0
15603246    Female  27  57000           0

Tags: 数据idagegendermalefemalepurchasedestimatedsalary
2条回答

用途:

df.loc[df['EstimatedSalary']< 21000, 'EstimatedSalary'] = 0

或:

df['EstimatedSalary'] = df['EstimatedSalary'].mask(df['EstimatedSalary'] < 21000, 0)

或:

df['EstimatedSalary'] = np.where(df['EstimatedSalary'] < 21000, 0, df['EstimatedSalary'])

print (df)
          D  Gender  Age  EstimatedSalary  Purchased
0  15624510    Male   19                0          0
1  15810944    Male   35                0          0
2  15668575  Female   26            43000          0
3  15603246  Female   27            57000          0

这是一种方法:

df.loc[df['EstimatedSalary'] < 21000, 'EstimatedSalary'] = 0

另一种利用boolint的子类这一事实的方法:

df['EsimatedSalary'] *= df['EstimatedSalary'] >= 21000

相关问题 更多 >

    热门问题