[英]Pandas: how to merge two dataframes on a column by keeping the information of the first one?
[英]how to merge two pandas DataFrames and aggregate one specific column
我有2个DataFrames:
city count school
0 New York 1 school_3
1 Washington 1 School_4
2 Washington 1 School_5
3 LA 1 School_1
4 LA 1 School_4
city count school
0 New York 1 School_3
1 Washington 1 School_1
2 LA 1 School_3
3 LA 2 School_4
我想得到这个结果:
city count school
0 New York 2 school_3
1 Washington 1 School_1
2 Washington 1 School_4
3 Washington 1 School_5
4 LA 1 School_1
5 LA 1 School_3
6 LA 3 School_4
以下是代码。
d1 = [{'city':'New York', 'school':'school_3', 'count':1},
{'city':'Washington', 'school':'School_4', 'count':1},
{'city':'Washington', 'school':'School_5', 'count':1},
{'city':'LA', 'school':'School_1', 'count':1},
{'city':'LA', 'school':'School_4', 'count':1}]
d2 = [{'city':'New York', 'school':'School_3', 'count':1},
{'city':'Washington', 'school':'School_1', 'count':1},
{'city':'LA', 'school':'School_3', 'count':1},
{'city':'LA', 'school':'School_4', 'count':2}]
x1 = pd.DataFrame(d1)
x2 = pd.DataFrame(d2)
#just get empty DataFrame
print pd.merge(x1, x2)
如何获得汇总结果?
你可以做:
>>> pd.concat([x1, x2]).groupby(["city", "school"], as_index=False)["count"].sum()
city school count
0 LA School_1 1
1 LA School_3 1
2 LA School_4 3
3 New York School_3 1
4 New York school_3 1
5 Washington School_1 1
6 Washington School_4 1
7 Washington School_5 1
请注意,纽约出现2次是因为数据中的拼写错误( school_3
vs School_3
)。
这是与使用pandas.DataFrame.merge(...)
@ elyase解决方案略有不同的实现
x1.merge(x2,on=['city', 'school', 'count'], how='outer').groupby(['city', 'school'], as_index=False)['count'].sum()
当在ipython notebook %timeit
定时时,此方法比@ elyase(<1ms)略快
100 loops, best of 3: 6.25 ms per loop #using concat(...) with @elyase's solution
100 loops, best of 3: 5.49 ms per loop #using merge(...) in this solution
此外,如果您想使用pandas aggregate
功能,您还可以:
x1.merge(x2,on=['city', 'school', 'count'], how='outer').groupby(['city', 'school'], as_index=False).agg(numpy.sum)
唯一的免责声明是使用agg(...)
是3种解决方案中最慢的。
显然所有3都提供了正确的结果:
city school count
0 LA School_1 1
1 LA School_3 1
2 LA School_4 3
3 New York School_3 1
4 New York school_3 1
5 Washington School_1 1
6 Washington School_4 1
7 Washington School_5 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.