首页 > 解决方案 > 根据条件将行更改为0

问题描述

我有一个数据框:

   1     0      1         2
   2     2      3         1
   3     8      2         9 

预期输出:

       1     0      1         0
       2     2      3         0
       3     8      2         9 
    

我最初的思考过程是

     df = np.where(df['Reported'] >= df['Goal'], df['Score'] = 0, df['Score'] 

这不能按需要工作。

标签: pythonpandasnumpy

解决方案


您可以使用loc索引方法:

df.loc[df['Reported'] >= df['Goal'],'Score'] = 0

推荐阅读