非常方便由于计算某一个量的出现次数
下面代码中count列即每个年月的出现次数
1 test_sample['yearmonth'] = test_sample['createtime'].apply(fenge) 2 test_sample['jishu'] = 1 3 tmp = test_sample.groupby(['yearmonth'])['jishu'].agg(['count']).reset_index()
groupby 中的as_index参数,默认为True,即将groupby中的列作为索引,
as_index = False时,使用原索引。
1 df = pd.DataFrame(data={'books': ['bk1', 'bk1', 'bk1', 'bk2', 'bk2', 'bk3'], 2 'price': [12, 12, 12, 15, 15, 17], 3 'title':['语文','语文','语文','数学','数学','英语'], 4 'weight':[2,2,2,4,4,8]}) 5 print(df) 6 df1 = df.groupby('books', as_index=True).sum() # sum()函数对其它是数字的列进行求和,字符串的列不会被输出 7 print(df1.columns) 8 print(type(df1)) # 此时的列只有'price', 'weight',books作为了索引列 9 print(df1) 10 df2 = df.groupby('books', as_index=False).sum() 11 print(df2.columns) 12 print(df2)
输出:
1 books price title weight
2 0 bk1 12 语文 2
3 1 bk1 12 语文 2
4 2 bk1 12 语文 2 5 3 bk2 15 数学 4 6 4 bk2 15 数学 4 7 5 bk3 17 英语 8 8 Index(['price', 'weight'], dtype='object') 9 <class 'pandas.core.frame.DataFrame'> 10 price weight 11 books 12 bk1 36 6 13 bk2 30 8 14 bk3 17 8 15 Index(['books', 'price', 'weight'], dtype='object') 16 books price weight 17 0 bk1 36 6 18 1 bk2 30 8 19 2 bk3 17 8
参考: https://blog.csdn.net/cjsyr6wt/article/details/78200444