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

标签 python python-2.7 pandas inner-join

有两个日期帧,一个是 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/

相关文章:

python - Python 中的常用日志记录模块

linux - 我如何在 python 中获得 unix 中的最大文件系统路径长度?

python - 循环遍历 python 字典并操作每个值

python - 根据 Python Pandas 中的相邻行计算值

python - 如何防止在 PySide 中使用 QTableWidget 删除项目时插入新行

python - 如何使用 urlopen() 在 Python 中下载文件或向 urlretrieve() 添加自定义 header ?

python - 如何从 Windows 命令行运行 python 文件 (.py) 而不必先键入 python?

python - 基于 Pandas Dataframe 中的多列计算公式 - 但不创建许多中间列

python - 未成功追加到空 NumPy 数组

python - 如何修复 "zeep.exceptions.ValidationError: Missing element Value"