pandas在groupby之后获得整数键

时间:2018-05-25 10:00:46

标签: python python-3.x pandas dataframe pandas-groupby

我有一个包含多列的groupby,并且键包含使输出难以阅读的所有列...这是一个示例

import pandas as pd
import numpy as np
from pandas import Series

df = pd.DataFrame({'A': [1, 1, 2, 2],
                   'B': [1, 2, 2, 2],
                   'C': np.random.randn(4),
                   'D': ['one', 'two', 'three', 'four']})

def aggregate(x):
    return Series(dict(C=round(x['C'].mean()), D=' '.join(x['D'])))

print(df.groupby(['A', 'B']).apply(aggregate))
       C           D
A B                 
1 1  0.0         one
  2 -1.0         two
2 2 -0.0  three four

如何获得'普通'键?像

   C           D
0  0.0         one
1 -1.0         two
2 -0.0  three four

2 个答案:

答案 0 :(得分:1)

您可以使用reset_index并指定可选参数drop=True。请注意,这会完全删除您的分组键索引。

print(df.groupby(['A', 'B']).apply(aggregate).reset_index(drop=True))

   C           D
0  0         one
1 -1         two
2  0  three four

答案 1 :(得分:1)

要获得更好的效果,最好dictionary使用DataFrameGroupBy.agg,最后添加reset_index drop=True以删除MultiIndex

aggregate = {'C':lambda x: round(x.mean()), 'D':' '.join}
print(df.groupby(['A', 'B']).agg(aggregate).reset_index(drop=True))
     C           D
0  0.0         one
1  0.0         two
2  1.0  three four

如果希望MultiIndex转换为columns,则有两种方式:

print(df.groupby(['A', 'B'], as_index=False).agg(aggregate))

或者:

print(df.groupby(['A', 'B']).agg(aggregate).reset_index())
   A  B    C           D
0  1  1  0.0         one
1  1  2 -1.0         two
2  2  2 -1.0  three four