Python 如何使用np.where更改多个列值?

Python 如何使用np.where更改多个列值?,python,pandas,numpy,Python,Pandas,Numpy,假设您拥有以下df: d = {'line amount#1': [0.21, 0.13, 0.1], 'line amount#2': [0.0, 0.05, .05], 'ExclBTW': [0.5, 0.18, .05]} dftaxitems = pd.DataFrame(data=d) dftaxitems line amount#1 line amount#2 ExclBTW 0 0.21 0.00 0.50 1 0.13

假设您拥有以下df:

d = {'line amount#1': [0.21, 0.13, 0.1], 'line amount#2': [0.0, 0.05, .05], 'ExclBTW': [0.5, 0.18, .05]}
dftaxitems = pd.DataFrame(data=d)
dftaxitems


line amount#1   line amount#2   ExclBTW
0   0.21                0.00    0.50
1   0.13                0.05    0.18
2   0.10                0.05    0.05
现在,我想将行金额的所有值都更改为np.nan(当它们不加在BTW列中时),并在它们加在一起时保留该值


所以我想动态地做这件事,因为行的数量可能多达10行

但是,使用以下代码获取以下错误:

#change line amount if not totalling into ExclBTW:
dfchecklineamount = dftaxitems.filter(like='line amount').astype(float)
dfchecklineamount['sum'] = dfchecklineamount[list(dfchecklineamount.columns)].sum(axis=1)
dfchecklineamount['check'] = np.where(dfchecklineamount['sum'].astype(float) == dfresult['ExclBTW'].astype(float),True, False)

dfchecklineamount['check'] = np.where(dfchecklineamount['sum'].astype(float) == dfresult['ExclBTW'].astype(float),True, False)
colstochange = dfchecklineamount.filter(regex ='line amount').columns
dfchecklineamount[colstochange] = np.where(dfchecklineamount['check'] == False, np.nan,dfchecklineamount[colstochange] )







 ValueError: operands could not be broadcast together with shapes (2,) () (2,4) 
请帮忙

期望输出:

line amount#1   line amount#2   ExclBTW
0   np.nan             np.nan    0.50
1   0.13                0.05     0.18
2   np.nan             np.nan     0.05
我用这个,但一定有更好的方法来解决它

import pandas as pd
import numpy as np

d = {'line amount#1': [0.21, 0.13, 0.1], 'line amount#2': [0.0, 0.05, .05], 'ExclBTW': [0.5, 0.18, .05]}
df = pd.DataFrame(data=d)
print(df)

df['line amount#1'] = df['line amount#1'].where(df['line amount#1'] + df['line amount#2'] == df['ExclBTW'], other=np.nan)
df['line amount#2'] = df['line amount#1'].where(df['line amount#1'] + df['line amount#2'] == df['ExclBTW'], other=np.nan)
print(df)
输出:

   line amount#1  line amount#2  ExclBTW
0           0.21           0.00     0.50
1           0.13           0.05     0.18
2           0.10           0.05     0.05

   line amount#1  line amount#2  ExclBTW
0            NaN            NaN     0.50
1           0.13           0.13     0.18
2            NaN            NaN     0.05
我们可以使用
sum
over
axis=1
将值设置为
NaN

编辑:添加更通用的解决方案,该解决方案可处理任意数量的类似lineamount列

c = dftaxitems.filter(like='line amount#').columns
m = dftaxitems[c].sum(1).eq(dftaxitems['ExclBTW'])
dftaxitems.loc[~m, c] = np.nan
dftaxitems


你能试试下面的吗。简单的解释是:我们可以使用布尔索引来填充NaN值。首先在m变量中获取掩码(在这里,我们检查两列之和是否等于第三列的条件),然后使用loc函数相应地设置NaN值

m = (dftaxitems['line amount#1'] + dftaxitems['line amount#2']) == dftaxitems['ExclBTW']

dftaxitems.loc[~m, ['line amount#1', 'line amount#2']] = np.nan
dftaxitems
输出如下:

   line amount#1  line amount#2  ExclBTW
0            NaN            NaN     0.50
1           0.13           0.05     0.18
2            NaN            NaN     0.05

请发布您预期的输出数据框。这有帮助吗?可以吗@sammywemmy@AmriRasyidi不幸的是,这是一列。您确定预期行的最后一行是NaN吗?0.1+0.05>=0.05所以我想动态地做,因为行数最多可以有10行。谢谢你的回答:你能不能让行数有点动态,因为我想动态地做,因为行数最多可以有10行。@Max,当然,请给我一个min或2,我会发布它。@Max,请检查我的编辑答案,并让我知道这是否对您有帮助。@Erfan,可能逻辑是相同的(可能对许多人来说),但实现是完全不同的,这不能说是复制IMHO,干杯。谢谢您的帮助@RavinderSingh13!这似乎是一个很好的解决方案,但结果却不尽相同。。当你调整你的示例数据来重现问题时,行数是不应该的。现在给出的结果正确感谢您的解决方案我的值是非浮点值,因此必须使用。astype(float)
c = dftaxitems.filter(like='line amount#').columns
m = dftaxitems[c].sum(1).eq(dftaxitems['ExclBTW'])
dftaxitems.loc[~m, c] = np.nan
dftaxitems
m = (dftaxitems['line amount#1'] + dftaxitems['line amount#2']) == dftaxitems['ExclBTW']

dftaxitems.loc[~m, ['line amount#1', 'line amount#2']] = np.nan
dftaxitems
   line amount#1  line amount#2  ExclBTW
0            NaN            NaN     0.50
1           0.13           0.05     0.18
2            NaN            NaN     0.05