合并和转换两个 pandas 数据帧

Merging and transforming two pandas dataframes

我有两个 pandas 数据框:

格式为:

type sum date
x1   12  01/01/12
x2   10  01/01/12
x3   8   01/01/12
x1   13  02/01/12
x2   12  02/01/12
x3   55  02/01/12
x1   11  03/01/12
x2   10  03/01/12
x3   8   03/01/12

和另一个格式

total date
122   01/01/12
133   02/01/12
144   03/01/12

将这些组合起来的最简单方法是什么,以便我可以获得以下输出:

date     x1 x2 x3 total
01/01/12 12 10 8  122
02/01/12 13 12 55 133
03/01/12 11 10 8  144

我尝试了很多功能,但都变得非常混乱,速度非常快,而且似乎不起作用。

如有任何帮助,我们将不胜感激。

您可以使用 pivot with df1, set_index with df2 and then concat them together. Last you can remove columns name and reset_index:

print df1.pivot(index='date', columns='type', values='sum')
type        x1  x2  x3
date                  
2012-01-01  12  10   8
2012-02-01  13  12  55
2012-03-01  11  10   8

print df2.set_index('date')
            total
date             
2012-01-01    122
2012-02-01    133
2012-03-01    144

df = pd.concat([df1.pivot(index='date', columns='type', values='sum'), 
                df2.set_index('date')], axis=1)
df.columns.name = None
df = df.reset_index()
print df
        date  x1  x2  x3  total
0 2012-01-01  12  10   8    122
1 2012-02-01  13  12  55    133
2 2012-03-01  11  10   8    144

也许在您可以转换 DataFrames:

的列 date to_datetime 之前
df1['date'] = pd.to_datetime(df1['date'])
df2['date'] = pd.to_datetime(df2['date'])
print df1
  type  sum       date
0   x1   12 2012-01-01
1   x2   10 2012-01-01
2   x3    8 2012-01-01
3   x1   13 2012-02-01
4   x2   12 2012-02-01
5   x3   55 2012-02-01
6   x1   11 2012-03-01
7   x2   10 2012-03-01
8   x3    8 2012-03-01

print df2
   total       date
0    122 2012-01-01
1    133 2012-02-01
2    144 2012-03-01