格式化按两列分组的熊猫数据系列,并在第三个重新采样,平均值为 dict
Posted
技术标签:
【中文标题】格式化按两列分组的熊猫数据系列,并在第三个重新采样,平均值为 dict【英文标题】:Formatting pandas dataseries grouped by two columns and resampled on third with a mean to a dict 【发布时间】:2018-11-24 07:50:23 【问题描述】:我有一个像这样的数据框:
° item_name item_category scraping_date price
0 Michel1 Category1 2018-04-14 21.0
1 Michel1 Category1 2018-04-16 42.1
2 Michel1 Category1 2018-04-17 84.0
3 Michel1 Category1 2018-04-19 126.2
4 Michel1 Category1 2018-04-20 168.3
5 Michel1 Category2 2018-04-23 21.2
6 Michel1 Category2 2018-05-08 42.0
7 Michel1 Category2 2018-03-26 84.1
8 Michel1 Category2 2018-03-31 126.2
9 Michel1 Category2 2018-04-01 168.3
10 Michel2 Category1 2018-04-04 21.0
11 Michel2 Category1 2018-04-05 42.1
12 Michel2 Category1 2018-04-09 84.2
13 Michel2 Category1 2018-04-11 126.3
14 Michel2 Category1 2018-04-12 168.4
15 Michel2 Category2 2018-04-13 21.0
16 Michel2 Category2 2018-05-03 42.1
17 Michel2 Category2 2018-04-25 84.2
18 Michel2 Category2 2018-04-28 126.3
19 Michel2 Category2 2018-04-29 168.4
我想按项目名称和类别分组,按周重新采样并获得每周的平均价格。最后,我想在这样的字典中输出日期:
[
"item_name": "Michel1",
"item_category": "Category1",
"prices": [
"week": "1", "average": "84.2",
"week": "2", "average": "84.2"
]
,
"item_name": "Michel1",
"item_category": "Category2",
"prices": [
"week": "1", "average": "84.2",
"week": "2", "average": "84.2"
]
,....
]
我带了一些东西来分组并获得平均值,但我无法将其转换为字典:
df["price"] = df["price"].astype(float)
df["scraping_date"] = pd.to_datetime(df["scraping_date"])
df.set_index("scraping_date").groupby(["item_name","item_category"])["price"].resample("W").mean()
如果我执行.to_dict()
,我会得到这个,这几乎不是我想要的:
('Michel1', 'Category1', Timestamp('2017-12-03 00:00:00')): 20.0,
('Michel1', 'Category1', Timestamp('2017-12-10 00:00:00')): 20.0,
('Michel1', 'Category2', Timestamp('2017-12-17 00:00:00')): 20.0,
('Michel1', 'Category2', Timestamp('2017-12-24 00:00:00')): 20.0,
('Michel2', 'Category1', Timestamp('2017-12-31 00:00:00')): 20.0,
('Michel2', 'Category1', Timestamp('2018-01-07 00:00:00')): 20.0,
【问题讨论】:
这勾选了如何提出一个好问题的所有选项,干得好! 请分享您的原始数据框的to_dict()
。
【参考方案1】:
我不能保证速度,通过apply
使用group by
df['Week']=pd.to_datetime(df.scraping_date).dt.week
df.groupby(['item_name','item_category']).apply(lambda x : x.groupby(['Week']).price.mean().to_frame('average')
.reset_index().to_dict('r')).to_frame('price').reset_index().to_dict('r')
Out[51]:
['item_category': 'Category1',
'item_name': 'Michel1',
'price': ['Week': 15.0, 'average': 21.0,
'Week': 16.0, 'average': 105.15],
'item_category': 'Category2',
'item_name': 'Michel1',
'price': ['Week': 13.0, 'average': 126.2,
'Week': 17.0, 'average': 21.2,
'Week': 19.0, 'average': 42.0],
'item_category': 'Category1',
'item_name': 'Michel2',
'price': ['Week': 14.0, 'average': 31.55,
'Week': 15.0, 'average': 126.3],
'item_category': 'Category2',
'item_name': 'Michel2',
'price': ['Week': 15.0, 'average': 21.0,
'Week': 17.0, 'average': 126.3,
'Week': 18.0, 'average': 42.1]]
【讨论】:
@ScottBoston 谢谢你(我通常不使用 dict :-( 只能提供这种解决方案标志) @Wen 这简直太棒了,你这个摇滚小子!!感谢您提供快速、干净且有效的解决方案。关于速度,这应该不是一个大问题,因为我不会一次处理大量数据以上是关于格式化按两列分组的熊猫数据系列,并在第三个重新采样,平均值为 dict的主要内容,如果未能解决你的问题,请参考以下文章