从以下数据框架中,我想提供'传统'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
答案 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