Dataframe聚合到Dataframe

时间:2018-04-09 15:01:27

标签: python pandas

从以下数据框架中,我想提供'传统'DataFrame格式。这似乎很容易解决,但我很难得到它。

数据样本

import pandas as pd
df = pd.DataFrame({'col1':['A','A','A','A','B','B','B','A','C','C','C'],'col2':['foo','zzz','bar','foo','car','car','dog','bar','bar','zzz','bar']})
df

   col1 col2
0   A   foo
1   A   zzz
2   A   bar
3   A   foo
4   B   car
5   B   car
6   B   dog
7   A   bar
8   C   bar
9   C   zzz
10  C   bar

输出

df.groupby(['col1','col2'])['col1'].agg({'Frequency':'count'})

    Frequency
col1    col2    
A       bar  2
        foo  2
        zzz  1
B       car  2
        dog  1
C       bar  2
        zzz  1

期望输出

col1    col2   freq 
A       bar    2
A       foo    2
A       zzz    1
B       car    2
B       dog    1
C       bar    2
C       zzz    1

1 个答案:

答案 0 :(得分:2)

解决方案1:

尝试使用以下内容重新设置groupby上的索引:

df.groupby(['col1','col2'])['col1'].agg({'Frequency':'count'}).reset_index()

  col1 col2  Frequency
0    A  bar          2
1    A  foo          2
2    A  zzz          1
3    B  car          2
4    B  dog          1
5    C  bar          2
6    C  zzz          1

解决方案2

在创建as_index=False时使用groupby

df.groupby(['col1','col2'], as_index=False)['col1'].agg({'Frequency':'count'})

  col1 col2  Frequency
0    A  bar          2
1    A  foo          2
2    A  zzz          1
3    B  car          2
4    B  dog          1
5    C  bar          2
6    C  zzz          1