连接多个数据框

时间:2019-05-14 09:28:45

标签: python pandas dataframe join

我知道这个话题已经讨论过了,但是没有具体说明

假设我有4个具有相同列但时间范围(行)不同的数据帧。我将日期设置为索引,并希望将数据框合并为一个新的数据框。为此,我将数据框放入列表中,并尝试将它们加入循环中,但是我无法使其正常工作。

如果执行,则会出现以下错误:

ValueError:列重叠但未指定后缀:Index(['attr1','attr2'],dtype ='object'

默认情况下,“ join”应该在索引上进行联接,所以我想知道为什么会出现此错误?!

任何帮助将不胜感激。预先感谢。

以下是一些代码:

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.array([
    ['2019-04-29', 5, 9],
    ['2019-04-28', 4, 61],
    ['2019-04-27', 24, 9]]),
    columns=['Date', 'attr1', 'attr2'])
df1 = df1.set_index(['Date'])

df2 = pd.DataFrame(np.array([
    ['2019-04-25', 5, 19],
    ['2019-04-24', 14, 16],
    ['2019-04-23', 4, 9]]),
    columns=['Date', 'attr1', 'attr2'])
df2 = df2.set_index(['Date'])

df3 = pd.DataFrame(np.array([
    ['2019-04-29', 15, 49],
    ['2019-04-25', 4, 36],
    ['2019-04-23', 14, 9]]),
    columns=['Date', 'attr1', 'attr2'])
df3 = df3.set_index(['Date'])

df4 = pd.DataFrame(np.array([
    ['2019-04-29', 15, 49],
    ['2019-04-10', 4, 36],
    ['2019-04-5', 14, 9]]),
    columns=['Date', 'attr1', 'attr2'])
df4 = df4.set_index(['Date'])

dfs = [df1, df2, df3, df4]

for df in (dfs):
    df.join(df, how='outer')

所需格式如下:

df5 = pd.DataFrame(np.array([
['2019-04-29', 15, 49, 5, 19, 15, 49, 15, 49],
['2019-04-10', 4, 36, 14, 16, 4, 36, 4, 36],
['2019-04-5', 14, 9, 4, 36, 4, 36, 4, 36]]),
columns=['Date', 'attr1_x', 'attr2_x', 'attr1_y', 'attr2_y', 'attr1_z', 'attr2_z', 'attr1_v', 'attr2_v'])
df5 = df5.set_index(['Date'])

1 个答案:

答案 0 :(得分:1)

>>> from functools import reduce
>>> df_final = reduce(lambda left,right: pd.merge(left,right, how='outer', left_on='Date', right_on='Date'), dfs)
>>> df_final
        attr1_x attr2_x attr1_y attr2_y attr1_x attr2_x attr1_y attr2_y
Date                                                                      
2019-04-29       5       9     NaN     NaN      15      49      15      49
2019-04-28       4      61     NaN     NaN     NaN     NaN     NaN     NaN
2019-04-27      24       9     NaN     NaN     NaN     NaN     NaN     NaN
2019-04-25     NaN     NaN       5      19       4      36     NaN     NaN
2019-04-24     NaN     NaN      14      16     NaN     NaN     NaN     NaN
2019-04-23     NaN     NaN       4       9      14       9     NaN     NaN
2019-04-10     NaN     NaN     NaN     NaN     NaN     NaN       4      36
2019-04-5      NaN     NaN     NaN     NaN     NaN     NaN      14       9