Subtract 2 Pandas Dataframes retrieving “NaN”

泪湿孤枕 提交于 2020-01-04 13:12:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!