简体   繁体   English

熊猫分组两列并按平均值汇总

[英]pandas groupby two columns and summarize by mean

I have a data frame like this:我有一个这样的数据框:

df = pd.DataFrame()
df['id'] = [1,1,1,2,2,3,3,3,3,4,4,5]
df['view'] = ['A', 'B', 'A', 'A','B', 'A', 'B', 'A', 'A','B', 'A', 'B']
df['value'] = np.random.random(12)


    id view     value
0    1    A  0.625781
1    1    B  0.330084
2    1    A  0.024532
3    2    A  0.154651
4    2    B  0.196960
5    3    A  0.393941
6    3    B  0.607217
7    3    A  0.422823
8    3    A  0.994323
9    4    B  0.366650
10   4    A  0.649585
11   5    B  0.513923

I now want to summarize for each id each view by mean of 'value'.我现在想通过“值”来总结每个view的每个id Think of this as some ids have repeated observations for view, and I want to summarize them.把这想成是一些id重复观察以供查看,我想总结一下。 For example, id 1 has two observations for A.例如,id 1 对 A 有两个观察值。

I tried我试过

res = df.groupby(['id', 'view'])['value'].mean()

This actually almost what I want, but pandas combines the id and view column into one, which I do not want.这实际上几乎是我想要的,但是 pandas 将idview列合二为一,这是我不想要的。

id  view
1   A       0.325157
    B       0.330084
2   A       0.154651
    B       0.196960
3   A       0.603696
    B       0.607217
4   A       0.649585
    B       0.366650
5   B       0.513923

also res.shape is of dimension (9,) res.shape 也是维度 (9,)

my desired output would be this:我想要的输出是这样的:

id  view    value
1   A       0.325157
1   B       0.330084
2   A       0.154651
2   B       0.196960
3   A       0.603696
3   B       0.607217
4   A       0.649585
4   B       0.366650
5   B       0.513923

where the column names and dimensions are kept and where the id is repeated.保留列名和维度的位置以及重复 id 的位置。 Each id should have only 1 row for A and B.每个 id 应该只有 1 行用于 A 和 B。

How can I achieve this?我怎样才能做到这一点?

You need reset_index or parameter as_index=False in groupby , because you get MuliIndex and by default the higher levels of the indexes are sparsified to make the console output a bit easier on the eyes:您需要在groupby reset_index或参数as_index=False ,因为您获得了MuliIndex并且默认情况下,索引的较高级别会被稀疏化,以使控制台输出在眼睛上更容易一些:

np.random.seed(100)
df = pd.DataFrame()
df['id'] = [1,1,1,2,2,3,3,3,3,4,4,5]
df['view'] = ['A', 'B', 'A', 'A','B', 'A', 'B', 'A', 'A','B', 'A', 'B']
df['value'] = np.random.random(12)
print (df)
    id view     value
0    1    A  0.543405
1    1    B  0.278369
2    1    A  0.424518
3    2    A  0.844776
4    2    B  0.004719
5    3    A  0.121569
6    3    B  0.670749
7    3    A  0.825853
8    3    A  0.136707
9    4    B  0.575093
10   4    A  0.891322
11   5    B  0.209202
res = df.groupby(['id', 'view'])['value'].mean().reset_index()
print (res)
   id view     value
0   1    A  0.483961
1   1    B  0.278369
2   2    A  0.844776
3   2    B  0.004719
4   3    A  0.361376
5   3    B  0.670749
6   4    A  0.891322
7   4    B  0.575093
8   5    B  0.209202

res = df.groupby(['id', 'view'], as_index=False)['value'].mean()
print (res)
   id view     value
0   1    A  0.483961
1   1    B  0.278369
2   2    A  0.844776
3   2    B  0.004719
4   3    A  0.361376
5   3    B  0.670749
6   4    A  0.891322
7   4    B  0.575093
8   5    B  0.209202

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM