合并数据帧中的熊猫“多索引”问题

时间:2019-02-22 13:57:39

标签: pandas dataframe indexing merge multi-index

我的面板数据集为df

stock    year    date   return
VOD      2017    01-01   0.05
VOD      2017    01-02   0.03
VOD      2017    01-03   0.04
...      ...     ...     ....
BAT      2017    01-01   0.05
BAT      2017    01-02   0.07
BAT      2017    01-03   0.10

所以我使用此代码来获取每年每只股票的收益均值和偏度。

df2=df.groupby(['stock','year']).mean().reset_index()
df3=df.groupby(['stock','year']).skew().reset_index()

df2df3看起来不错。

df2就像(在我更改列名之后)

stock    year   mean_return
 VOD     2017    0.09
 BAT     2017    0.14
 ...      ...    ...

df3就像(在我更改列名之后)

stock    year   return_skewness
 VOD     2017    -0.34
 BAT     2017    -0.04
 ...      ...    ...

问题是当我尝试使用合并df2df3

want=pd.merge(df2,df2, on=['stock','year'],how='outer')

python给了我

'The column label 'stock' is not unique.
For a multi-index, the label must be a tuple with elements corresponding to each level.'

,这让我很困惑。

我可以使用want = pd.merge(df2,df3, left_index=True, right_index=True, how='outer')合并df2df3,但是之后我必须重命名列,因为列名在括号中。

是否有任何方便的方法来合并df2df3?谢谢

2 个答案:

答案 0 :(得分:2)

最好使用agg在列表和列中指定聚合函数,以便在函数之后进行聚合:

df3 = (df.groupby(['stock','year'])['return']
         .agg([('mean_return','mean'),('return_skewness','skew')])
         .reset_index())
print (df3)
  stock  year  mean_return  return_skewness
0   BAT  2017     0.073333         0.585583
1   VOD  2017     0.040000         0.000000

您的解决方案应通过删除reset_indexrename和最后一个concat进行更改,并在汇总的列return中指定:

s2=df.groupby(['stock','year'])['return'].mean().rename('mean_return')
s3=df.groupby(['stock','year'])['return'].skew().rename('return_skewness')

df3 = pd.concat([s2, s3], axis=1).reset_index()
print (df3)
  stock  year  mean_return  return_skewness
0   BAT  2017     0.073333         0.585583
1   VOD  2017     0.040000         0.000000

编辑:

如果需要汇总所有数字列,请先在groupby之后删除列表,然后将mapjoin一起用于展平MultiIndex

print (df)
  stock  year   date  return  col
0   VOD  2017  01-01    0.05    1
1   VOD  2017  01-02    0.03    8
2   VOD  2017  01-03    0.04    9
3   BAT  2017  01-01    0.05    1
4   BAT  2017  01-02    0.07    4
5   BAT  2017  01-03    0.10    3

df3 = df.groupby(['stock','year']).agg(['mean','skew'])
print (df3)
              return                 col          
                mean      skew      mean      skew
stock year                                        
BAT   2017  0.073333  0.585583  2.666667 -0.935220
VOD   2017  0.040000  0.000000  6.000000 -1.630059

df3.columns = df3.columns.map('_'.join)
df3 = df3.reset_index()
print (df3)
  stock  year  return_mean  return_skew  col_mean  col_skew
0   BAT  2017     0.073333     0.585583  2.666667 -0.935220
1   VOD  2017     0.040000     0.000000  6.000000 -1.630059

您的解决方案应该更改:

df2=df.groupby(['stock','year']).mean().add_prefix('mean_')
df3=df.groupby(['stock','year']).skew().add_prefix('skew_')


df3 = pd.concat([df2, df3], axis=1).reset_index()
print (df3)
  stock  year  mean_return  mean_col  skew_return  skew_col
0   BAT  2017     0.073333  2.666667     0.585583 -0.935220
1   VOD  2017     0.040000  6.000000     0.000000 -1.630059

答案 1 :(得分:1)

一种更简单的方法来绕过此问题:

df2.to_clipboard(index=False)
df2clip=pd.read_clipboard(sep='\t')

df3.to_clipboard(index=False)
df3clip=pd.read_clipboard(sep='\t')

然后再次合并2 df:

pd.merge(df2clip,df3clip,on=['stock','year'],how='outer')
相关问题