我在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
答案 0 :(得分:1)
这是一种方式:
df.loc[df['EstimatedSalary'] < 21000, 'EstimatedSalary'] = 0
利用bool
是int
的子类这一事实的另一种方式:
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