如何更新DataFrame中的特定值

时间:2018-02-22 12:12:38

标签: python pandas

我在python中使用pandas。

如何通过'EstimatedSalary'将数据帧的所有值设置为低于21000?意味着我希望前两行有零而不是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

2 个答案:

答案 0 :(得分:1)

这是一种方式:

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

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

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

答案 1 :(得分:1)

使用:

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