pandas与所有dataframe列中的聚合统计信息分组

时间:2015-06-25 11:40:36

标签: python pandas aggregation pandas-groupby

我正在将数据分组到pandas数据帧中,并使用一些聚合函数来生成结果数据。输入数据:

A  B  C  D  E  F
0  aa  5  3  2  2  2
1  aa  3  2  2  3  3
2  ac  2  0  2  7  7
3  ac  9  2  3  8  8
4  ac  2  3  7  3  3
5  ad  0  0  0  1  1
6  ad  9  9  9  9  9
7  ad  6  6  6  6  6
8  ad  3  3  3  3  3

pandas分组函数似乎一次只能在一列上运行,但我想在我的df中的所有列上生成统计信息。例如,我可以使用功能分组['C']。agg([np.mean,len])来生成列'C'的统计信息,但是如果我想在所有列A-F上生成这些统计信息呢?

这个输出是:

    A  count_C    mean_C
0  aa        2  2.500000
1  ac        3  1.666667
2  ad        4  4.500000

但我想要的是:

    A  count_B    mean_B  count_C    mean_C  count_D  mean_D  etc...
0  aa        2  4.000000        2  2.500000        2     2.0  etc...
1  ac        3  4.333333        3  2.500000        3     4.0
2  ad        4  4.500000        4  2.500000        4     4.5

在单个命令中使用聚合是否有简单的方法来执行组?如果没有,是否有一种简单的方法来迭代所有列并合并每列的新聚合统计结果?

到目前为止,这是我的完整代码:

import pandas as pd
import numpy as np
import pprint as pp

test_dataframe = pd.DataFrame({
    'A' : ['aa', 'aa', 'ac', 'ac', 'ac', 'ad', 'ad', 'ad', 'ad'],
    'B' : [5, 3, 2, 9, 2, 0, 9, 6, 3],
    'C' : [3, 2, 0, 2, 3, 0, 9, 6, 3],
    'D' : [2, 2, 2, 3, 7, 0, 9, 6, 3],
    'E' : [2, 3, 7, 8, 3, 1, 9, 6, 3],
    'F' : [2, 3, 7, 8, 3, 1, 9, 6, 3]
})

#group, aggregate, convert object to df, sort index
grouped = test_dataframe.groupby(['A'])
grouped_stats = grouped['C'].agg([np.mean, len])
grouped_stats = pd.DataFrame(grouped_stats).reset_index()
grouped_stats.rename(columns = {'mean':'mean_C', 'len':'count_C'}, inplace=True)
grouped_stats.sort_index(axis=1, inplace=True)

print "Input: "
pp.pprint(test_dataframe)

print "Output: "
pp.pprint(grouped_stats)

1 个答案:

答案 0 :(得分:0)

您不必逐个调用grouped['B'] grouped['C'],只需传递整个groupby对象,pandas就会将聚合函数应用于所有列。

import pandas as pd

test_dataframe = pd.DataFrame({
    'A' : ['aa', 'aa', 'ac', 'ac', 'ac', 'ad', 'ad', 'ad', 'ad'],
    'B' : [5, 3, 2, 9, 2, 0, 9, 6, 3],
    'C' : [3, 2, 0, 2, 3, 0, 9, 6, 3],
    'D' : [2, 2, 2, 3, 7, 0, 9, 6, 3],
    'E' : [2, 3, 7, 8, 3, 1, 9, 6, 3],
    'F' : [2, 3, 7, 8, 3, 1, 9, 6, 3]
})
agg_funcs = ['count', 'mean']
test_dataframe = test_dataframe.groupby(['A']).agg(agg_funcs)

columns = 'B C D E F'.split()
names = [y + '_' + x for x in columns for y in agg_funcs]
test_dataframe.columns = names

Out[89]: 
    count_B  mean_B  count_C  mean_C  count_D  mean_D  count_E  mean_E  count_F  mean_F
A                                                                                      
aa        2  4.0000        2  2.5000        2     2.0        2    2.50        2    2.50
ac        3  4.3333        3  1.6667        3     4.0        3    6.00        3    6.00
ad        4  4.5000        4  4.5000        4     4.5        4    4.75        4    4.75