我正试图按月和用户对产品数量进行分组。我有每天的数据,所以首先我有分组的几个月,然后每个用户。见下表:
Date UserID Product
2016-02-02 1 Chocolate
2016-03-03 22 Chocolate
2016-03-03 22 Banana
2016-03-03 22 Banana
2016-03-03 22 Chocolate
2016-04-03 22 Chocolate
2016-04-03 22 Banana
2016-04-03 33 Banana
2016-04-03 33 Chocolate
2016-04-03 22 Peanuts
2016-04-03 33 Peanuts
2016-04-03 33 Peanuts我的结果应该是:
Date UserID Product Count
2016-03 22 Banana 2
2016-03 22 Chocolate 2
2016-04 22 Banana 1
2016-04 22 Peanuts 1
2016-04 33 Banana 1
2016-04 33 Peanuts 2
2016-4 33 Chocolate 1我需要和巨蟒熊猫一起做这件事,但我不能
用这个代码
dfcount = df(['Date','UserID','Product']).Kit.count()我确实得到了一个数字,但每天,我如何能做到每月?
我试过这个:
df[['Date', 'UserID', 'Product']].groupby(pd.Grouper(key='Date', freq='1M')).sum().sort_values(by='Date', ascending=True)['Product']它不起作用
它返回它不识别我的产品列,但是我的分组可能是错误的。
KeyError:“产品”
发布于 2019-06-16 00:19:13
我首先将该列转换为Datetime,因为这样可以轻松提取年份/月/日(通过执行df.<date column>.dt.<year/month/day>)。
df['Date'] = df.Date.apply(lambda x: pd.to_datetime(x, format='%Y-%m-%d'))然后,按月、客户和产品分组:
counts = (df.groupby([df.Date.dt.month,
'UserID',
'Product']).count())
print(counts)
Date
Date UserID Product
2 1 Chocolate 1
3 22 Banana 2
Chocolate 2
4 22 Banana 1
Chocolate 1
Peanuts 1
33 Banana 1
Chocolate 1
Peanuts 2在这里,如果您获得的数据跨度超过一年,上述解决方案允许您仍然按月分组。如果您想在这个新的扩展数据集中按年和按月对产品和用户进行分组,您可以简单地将年份提取添加到您的群中,如下所示:
counts = (df.groupby([df.Date.dt.year,
df.Date.dt.month,
'UserID',
'Product']).count())
print(counts)
Date
Date Date UserID Product
2016 2 1 Chocolate 1
3 22 Banana 2
Chocolate 2
4 22 Banana 1
Chocolate 1
Peanuts 1
33 Banana 1
Chocolate 1
Peanuts 2
2017 2 1 Chocolate 1
3 22 Banana 2
Chocolate 1这样,您将更加清楚地了解如何对数据进行分组(因此,以后不太可能得到意想不到的结果)。
发布于 2019-06-15 23:29:48
如果Date是字符串,则可以
df.groupby([df.Date.str[:7], 'UserID', 'Product']).count()
Date
Date UserID Product
2016-02 1 Chocolate 1
2016-03 22 Banana 2
Chocolate 2
2016-04 22 Banana 1
Chocolate 1
Peanuts 1
33 Banana 1
Chocolate 1
Peanuts 2有一个datetime列:
df.groupby([df.Date.dt.to_period('M'), 'UserID', 'Product']).count()发布于 2019-06-15 23:30:36
df['Date'] = pd.to_datetime(df.Date).dt.to_period('1M')
df['Count'] = 1
df.groupby(by=['Date','UserID','Product']).agg({'Count':'sum'}).reset_index().sort_values(by=['Date','UserID'])输出:
+---+---------+--------+-----------+-------+
| | Date | UserID | Product | Count |
+---+---------+--------+-----------+-------+
| 0 | 2016-02 | 1 | Chocolate | 1 |
| 1 | 2016-03 | 22 | Banana | 2 |
| 2 | 2016-03 | 22 | Chocolate | 2 |
| 3 | 2016-04 | 22 | Banana | 1 |
| 4 | 2016-04 | 22 | Chocolate | 1 |
| 5 | 2016-04 | 22 | Peanuts | 1 |
| 6 | 2016-04 | 33 | Banana | 1 |
| 7 | 2016-04 | 33 | Chocolate | 1 |
| 8 | 2016-04 | 33 | Peanuts | 2 |
+---+---------+--------+-----------+-------+https://stackoverflow.com/questions/56614711
复制相似问题