gpt4 book ai didi

Python Pandas : Combine two dataframes by date index and a common column value

转载 作者:行者123 更新时间:2023-11-30 22:32:22 24 4
gpt4 key购买 nike

有两个日期帧,一个是 df1,另一个是 df2,如下所示:

df1:

               a   b    id
2010-01-01 1 4 21
2010-01-01 2 5 22
2010-01-01 3 6 23
2010-01-01 4 7 24
2010-01-02 1 4 21
2010-01-02 2 5 22
2010-01-02 3 6 23
2010-01-02 4 7 24
2010-01-03 1 4 21
2010-01-03 2 5 22
2010-01-03 3 6 23
2010-01-03 4 7 24
...........................

df2:

               c   d    id
2010-01-02 1 4 21
2010-01-02 2 5 22
2010-01-02 3 6 23
2010-01-02 4 7 24
2010-01-03 1 4 21
2010-01-03 2 5 22
2010-01-03 3 6 23
2010-01-03 4 7 24
...........................

我想通过公共(public)索引(请注意,df1 中的某些索引不在 df2 中)和 id 合并或连接两个数据帧,并且我期望连接的数据帧如下

               c   d    a   b   id
2010-01-02 1 4 1 4 21
2010-01-02 2 5 2 5 22
2010-01-02 3 6 3 6 23
2010-01-02 4 7 4 7 24
2010-01-03 1 4 1 4 21
2010-01-03 2 5 2 5 22
2010-01-03 3 6 3 6 23
2010-01-03 4 7 4 7 24

我使用了以下代码

 df = df1.join(df2, on = ['id'], how='inner')

但这没有用

最佳答案

IIUC:

In [388]: df2.set_index('id', append=True).join(df1.set_index('id', append=True)) \
.reset_index(level='id')
Out[388]:
id c d a b
2010-01-02 21 1 4 1 4
2010-01-02 22 2 5 2 5
2010-01-02 23 3 6 3 6
2010-01-02 24 4 7 4 7
2010-01-03 21 1 4 1 4
2010-01-03 22 2 5 2 5
2010-01-03 23 3 6 3 6
2010-01-03 24 4 7 4 7

关于Python Pandas : Combine two dataframes by date index and a common column value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45503965/

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