问题
I have 2 pandas dataframes.
A = pd.DataFrame({'c1':[1],'c2':[2],'c3':[2],'c4':[1],'c5':[1],'c6':[1]})
B = pd.DataFrame({'c1':[0],'c2':[1],'c3':[0],'c4':[1],'c5':[0],'c6':[1]})
I would like to subtract B to A and assign the difference to A. I tried several options wither using the assign subtraction or pandas subtraction functions, but don't seem to get the right values.
Either get "NaN" when subtracting and assigning or the wrong data.
I tried:
A.sub(B, fill_value=0)
and
A-=B
I expect, after this iteration, to get a new A dataframe [1 , 1 , 2 , 0 , 1 , 0 ].
Any ideas?
回答1:
Just use subtract
A.subtract(B).fillna(0)
c1 c2 c3 c4 c5 c6
0 1 1 2 0 1 0
来源:https://stackoverflow.com/questions/57461787/subtract-2-pandas-dataframes-retrieving-nan