[英]How to subtract two dataframes , partial common indexes
I have two dataframes a1,a2 with the same columns and indexes , but with different rows . 我有两个具有相同的列和索引,但具有不同的行的数据框a1,a2。
a1:
f d r f
0 50.1 0 -1.374201 35
1 50.2 1 1.415697 29
2 70 3 0.233841 18
3 80 4 1.550599 30
4 90.2 5 -0.178370 63
a2:
f d r f
2 25 3 0.233841 18
3 95 4 1.550599 30
I want to subtract a1 from a2, so a3=a2-a1 , 我想从a2减去a1,所以a3 = a2-a1,
So I will received: 所以我会收到:
a3:
f d r f
2 -45 0 0 0
3 15 4 0 0
Thanks, 谢谢,
It's fairly simple, first you do simple substraction and you'll get a 5x4 DataFrame
that will only have results in the rows with the same indices, others will be NaN
. 这很简单,首先进行简单的减法
DataFrame
,您将获得一个5x4的DataFrame
,该DataFrame
仅在具有相同索引的行中具有结果,其他将为NaN
。 Then you drop NaN
values: 然后删除
NaN
值:
a3 = a2-a1
a3.dropna(inplace=True)
I'm not sure why in your desired DataFrame
you want d
at index 3 to equal 4. 我不确定为什么要在所需的
DataFrame
让索引3的d
等于4。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.