Imagine you have the following 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
Now, I want to change all values of line amounts to np.nan when they do not add up to the BTW column and keep the value when they do add up.
So I want to do it dynamically because the line amounts may be up to 10 lines large.
However getting the following error with the following code:
#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)
Please help!
Desired output:
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