2024-09-27 18:09:15 发布
网友
我用的是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
用途:
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
另一种利用bool是int的子类这一事实的方法:
bool
int
df['EsimatedSalary'] *= df['EstimatedSalary'] >= 21000
用途:
或:
或:
这是一种方法:
另一种利用
bool
是int
的子类这一事实的方法:相关问题 更多 >
编程相关推荐