Python 如何更新DataFrame中的特定值
我在python中使用熊猫 如何通过“EstimatedAlary”将21000以下的数据帧的所有值设置为零?这意味着我希望前两行有0,而不是19000和20000Python 如何更新DataFrame中的特定值,python,pandas,Python,Pandas,我在python中使用熊猫 如何通过“EstimatedAlary”将21000以下的数据帧的所有值设置为零?这意味着我希望前两行有0,而不是19000和20000 ID Gender Age EstimatedSalary Purchased 15624510 Male 19 19000 0 15810944 Male 35 20000 0 15668575 Female 26 43000
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
这是一种方式:
df.loc[df['EstimatedSalary'] < 21000, 'EstimatedSalary'] = 0
使用:
df.loc[df['EstimatedSalary']<21000,'EstimatedSalary']=0
或:
df['EstimatedSalary']=df['EstimatedSalary'].掩码(df['EstimatedSalary']<21000,0)
或:
df['EstimatedSalary']=np.where(df['EstimatedSalary']<21000,0,df['EstimatedSalary']))
打印(df)
D购买的性别年龄估计数
0 15624510男19 0 0
115810944男35 0 0
215668575女2643000
315603246女2757000
非常感谢您!它起作用了!我能在几分钟内把这个问题标记为被接受)@Fabi-谢谢。
df['EsimatedSalary'] *= df['EstimatedSalary'] >= 21000
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