pandas中的groupby()函数:选择某一列进行分组。(加上size()函数---统计本列中的元素出现频次)reset_index():重新分配索引
如:
import pandas as pd
df = pd.DataFrame({'key1':list('aabba'),
'key2': ['one','two','one','two','one'],
'data1': ['1','3','5','7','9'],
'data2': ['2','4','6','8','10']})
print(df.groupby(['key1']).size())
#输出结果:
key1
a 3
b 2
dtype: int64
可以看到此时输出结果是没有索引的,所加加上reset_index()
import pandas as pd
df = pd.DataFrame({'key1':list('aabba'),
'key2': ['one','two','one','two','one'],
'data1': ['1','3','5','7','9'],
'data2': ['2','4','6','8','10']})
print(df.groupby(['key1']).size().reset_index())
#输出结果:
key1 0
0 a 3
1 b 2