将具有重叠列名称的Pandas连接到多个数据框? [英] Joining Multiple Dataframes with Pandas with overlapping Column Names?

查看:121
本文介绍了将具有重叠列名称的Pandas连接到多个数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要合并多个(超过2个)数据帧.它们都共享相同的值列:

I have multiple (more than 2) dataframes I would like to merge. They all share the same value column:

In [431]: [x.head() for x in data]
Out[431]: 
[                     AvgStatisticData
DateTime                             
2012-10-14 14:00:00         39.335996
2012-10-14 15:00:00         40.210110
2012-10-14 16:00:00         48.282816
2012-10-14 17:00:00         40.593039
2012-10-14 18:00:00         40.952014,
                      AvgStatisticData
DateTime                             
2012-10-14 14:00:00         47.854712
2012-10-14 15:00:00         55.041512
2012-10-14 16:00:00         55.488026
2012-10-14 17:00:00         51.688483
2012-10-14 18:00:00         57.916672,
                      AvgStatisticData
DateTime                             
2012-10-14 14:00:00         54.171233
2012-10-14 15:00:00         48.718387
2012-10-14 16:00:00         59.978616
2012-10-14 17:00:00         50.984514
2012-10-14 18:00:00         54.924745,
                      AvgStatisticData
DateTime                             
2012-10-14 14:00:00         65.813114
2012-10-14 15:00:00         71.397868
2012-10-14 16:00:00         76.213973
2012-10-14 17:00:00         72.729002
2012-10-14 18:00:00         73.196415,
....etc

我读到联接可以处理多个数据帧,但是我得到了:

I read that join can handle multiple dataframes, however I get:

In [432]: data[0].join(data[1:])
...
Exception: Indexes have overlapping values: ['AvgStatisticData']

我尝试将rsuffix=["%i" % (i) for i in range(len(data))]传递给加入,但仍然遇到相同的错误.我可以通过以不重叠列名的方式构建data列表来解决此问题,但是也许有更好的方法吗?

I have tried passing rsuffix=["%i" % (i) for i in range(len(data))] to join and still get the same error. I can workaround this by building my data list in a way where the column names don't overlap, but maybe there is a better way?

推荐答案

In [65]: pd.concat(data, axis=1)
Out[65]:
                     AvgStatisticData  AvgStatisticData  AvgStatisticData  AvgStatisticData
2012-10-14 14:00:00         39.335996         47.854712         54.171233         65.813114
2012-10-14 15:00:00         40.210110         55.041512         48.718387         71.397868
2012-10-14 16:00:00         48.282816         55.488026         59.978616         76.213973
2012-10-14 17:00:00         40.593039         51.688483         50.984514         72.729002
2012-10-14 18:00:00         40.952014         57.916672         54.924745         73.196415

这篇关于将具有重叠列名称的Pandas连接到多个数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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