减去检索“NaN"的 2 个 Pandas 数据帧 [英] Subtract 2 Pandas Dataframes retrieving "NaN"

查看:39
本文介绍了减去检索“NaN"的 2 个 Pandas 数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个熊猫数据框.

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]})

我想将 B 减去 A 并将差值分配给 A.我使用分配减法或熊猫减法功能尝试了几个选项,但似乎没有得到正确的值.

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.

减法赋值时要么得到NaN",要么得到错误的数据.

Either get "NaN" when subtracting and assigning or the wrong data.

我试过了:

A.sub(B, fill_value=0)

A-=B

我希望在这次迭代之后得到一个新的 A 数据帧 [1 , 1 , 2 , 0 , 1 , 0 ].

I expect, after this iteration, to get a new A dataframe [1 , 1 , 2 , 0 , 1 , 0 ].

有什么想法吗?

推荐答案

只需使用减法

A.subtract(B).fillna(0)
  c1  c2  c3  c4  c5  c6
  0   1   1   2   0   1   0

这篇关于减去检索“NaN"的 2 个 Pandas 数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆