Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更新DataFrame中的特定值_Python_Pandas - Fatal编程技术网

Python 如何更新DataFrame中的特定值

Python 如何更新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

我在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           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