[英]Long to wide format with GroupBy
I have been trying to figure out how to do the following with no success. 我一直在尝试找出如何成功进行以下操作。 I tried sampling, grouping ... got stuck. 我尝试取样,分组...卡住了。
I have the following pandas dataframe example: 我有以下熊猫数据框示例:
date | price |
09/09/2018 08:30 | 22.1 |
09/09/2018 08:35 | 22.12 |
09/09/2018 08:40 | 22.20 |
09/09/2018 08:45 | 22.13 |
09/09/2018 08:50 | 22.19 |
09/09/2018 08:55 | 22.39 |
I want to group the dates in 15 minutes and get the following dataframe. 我想在15分钟内将日期分组并获得以下数据框。
date_15 | price_1 | price_2 | price 3 |
09/09/2018 08:30 | 22.1 | 22.12 | 22.20 |
09/09/2018 08:45 | 22.13 | 22.19 | 22.39 |
Then I would set_index('date_15') and do a join which I can do myself. 然后,我将set_index('date_15')进行自己可以做的联接。
Could you please help with the above? 您能帮上忙吗?
Do a groupby
on the date every 15 minutes using the pd.Grouper
, and then create a new DataFrame with the result: 做一个groupby
上的日期用每15分钟pd.Grouper
,然后创建结果的新数据帧:
df['date'] = pd.to_datetime(df.date, errors='coerce')
v = df.groupby(pd.Grouper(key='date', freq='15min'))['price'].apply(list)
pd.DataFrame(v.tolist(), index=v.index).add_prefix('price ')
price 0 price 1 price 2
date
2018-09-09 08:30:00 22.10 22.12 22.20
2018-09-09 08:45:00 22.13 22.19 22.39
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.