[英]Combine data frames while adding values in Pandas
New to Pandas and having trouble with something that seems simple. 对熊猫来说是新手,并且看起来很简单。 I've tried various merge and concat commands but haven't hit on the proper one yet.
我已经尝试了各种合并和concat命令,但尚未找到合适的命令。
Have two dfs that look like this: 有两个看起来像这样的dfs:
newstatus count()
0 field1 1
1 field2 7
2 field3 146
3 field4 94
[4 rows x 2 columns]
newstatus count()
0 field2 1
1 field3 32
2 field4 60
[3 rows x 2 columns]
What is the most efficient operation to combine them and add the count() field to get: 将它们组合起来并添加count()字段以获得最有效的操作是什么:
newstatus count()
0 field1 1
1 field2 8
2 field3 178
3 field4 154
[4 rows x 2 columns]
Note that the dfs don't necessarily have values for each field, ie field1 doesn't show up in the second df at all. 请注意,dfs不一定具有每个字段的值,即field1根本不会显示在第二个df中。
Not sure if this is the most efficient performance-wise, but I think it's the most straightforward: 不确定这是否是最有效的性能,但我认为这是最直接的:
>>> d1.set_index('newstatus').add(d2.set_index('newstatus'), fill_value=0).reset_index()
newstatus count
0 field1 1
1 field2 8
2 field3 178
3 field4 154
[4 rows x 2 columns]
It is slightly more awkward because you have newstatus
as a column rather than the index. 这有点尴尬,因为你有
newstatus
作为列而不是索引。 If you instead set your data up with newstatus
as the index, like this: 如果您使用
newstatus
作为索引来设置数据,如下所示:
count
newstatus
field1 1
field2 7
field3 146
field4 94
Then you can just do 然后就可以了
>>> d1.add(d2, fill_value=0)
count
newstatus
field1 1
field2 8
field3 178
field4 154
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.