如何将聚合输出合并回原始数据帧

时间:2018-06-02 22:48:50

标签: python pandas dataframe pandas-groupby

因此,对于方法的广义目的:

df= [['A' , 'B']] (dataframe is actually bigger but for simplicity)

SC = df[['A','B']].groupby('A').agg({'B': ['mean', 'std']})

我正在尝试将此输出恢复为原始df,因此输出可以是:

df=[['A','B','mean of B','std of B']]

我试过了:pd.merge(df, SC, on=None), 得到错误:

  

“MergeError:没有公共列来执行合并”

如果可能的话,可以非常感谢任何帮助。

谢谢

3 个答案:

答案 0 :(得分:5)

我认为你必须specify the columns to merge on

df.merge(SC, left_on = 'A', right_index=True)

示例:

# Original Dataframe (randomly created):
>>> df
   A  B
0  b  8
1  a  8
2  a  1
3  b  9
4  b  2
5  b  9
6  b  4
7  a  9
8  a  0
9  b  8

# The result of your "SC" object created by groupby and agg
>>> SC
          B          
       mean       std
A                    
a  4.500000  4.654747
b  6.666667  2.943920

# Merge them together on the appropriate columns:
>>> df.merge(SC, left_on = 'A', right_index=True)
   A  B  (B, mean)  (B, std)
0  b  8   6.666667  2.943920
3  b  9   6.666667  2.943920
4  b  2   6.666667  2.943920
5  b  9   6.666667  2.943920
6  b  4   6.666667  2.943920
9  b  8   6.666667  2.943920
1  a  8   4.500000  4.654747
2  a  1   4.500000  4.654747
7  a  9   4.500000  4.654747
8  a  0   4.500000  4.654747

如果需要,只需添加.sort_index即可按原始顺序获取合并的数据框:

df.merge(SC, left_on = 'A', right_index=True).sort_index()

答案 1 :(得分:3)

groupby转换

一种解决方案是执行两次groupby.transform计算:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0, 3, (50, 2)), columns=['A', 'B'])

df['mean'] = df.groupby('A')['B'].transform('mean')
df['std'] = df.groupby('A')['B'].transform('std')

print(df.head())

   A  B      mean       std
0  0  2  0.866667  0.915475
1  2  2  1.187500  0.910586
2  1  1  0.947368  0.911268
3  1  0  0.947368  0.911268
4  0  2  0.866667  0.915475

groupby agg + merge

或者,您可以执行单个groupby聚合。然后对齐索引并合并:

# grp dataframe is indexed by A
grp = df.groupby('A')['B'].agg(['mean', 'std'])

# set same index for df, perform merge on indices, then reset index
res = df.set_index('A')\
        .merge(grp, left_index=True, right_index=True)\
        .reset_index()

答案 2 :(得分:1)

您可以使用describe + reindex

df.assign(**df.groupby('A').B.apply(pd.Series.describe).unstack().reindex(df.A).to_dict('l')).head()
Out[171]: 
   A  B  25%  50%  75%  count  max      mean  min       std
0  1  2  0.0  1.0  2.0   19.0  2.0  1.052632  0.0  0.911268
1  1  0  0.0  1.0  2.0   19.0  2.0  1.052632  0.0  0.911268
2  1  2  0.0  1.0  2.0   19.0  2.0  1.052632  0.0  0.911268
3  2  0  0.0  1.0  2.0   18.0  2.0  1.000000  0.0  0.840168
4  1  2  0.0  1.0  2.0   19.0  2.0  1.052632  0.0  0.911268