gpt4 book ai didi

使用具有重叠列名的 Pandas 连接多个数据框?

转载 作者:行者123 更新时间:2023-12-02 14:50:07 25 4
gpt4 key购买 nike

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

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

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

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

我尝试过传递rsuffix=["%i" % (i) for i in range(len(data))]加入并仍然得到相同的错误。我可以通过构建我的 data 来解决这个问题以列名称不重叠的方式列出,但也许有更好的方法?

最佳答案

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 连接多个数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13003769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com