鉴于此样本时间序列:
price vol
2017-01-01 08:00:00 56 1544
2017-01-01 11:00:00 70 1680
2017-01-01 14:00:00 92 1853
2017-01-02 08:00:00 94 1039
2017-01-02 11:00:00 81 1180
2017-01-02 14:00:00 70 1443
2017-01-03 08:00:00 56 1621
2017-01-03 11:00:00 68 1093
2017-01-03 14:00:00 59 1684
2017-01-04 08:00:00 86 1591
df = df.resample('1d').mean()
给出平均值并且
df = df.resample('1d').max()
给出了最大值,但有没有办法一步获得这两个值?
如何重新采样该数据框,以便输出包含价格和交易量的每日平均值以及当天价格和交易量的每日最大值?
输出列应为index、price_mean、vol_mean、price_max、vol_max,数据应为每日
import pandas as pd
df = pd.DataFrame({'price': [56, 70, 92, 94, 81, 70, 56, 68, 59, 86],
'vol': [1544, 1680, 1853, 1039, 1180, 1443, 1621, 1093, 1684, 1591]},
index=pd.to_datetime(['2017-01-01 08:00:00', '2017-01-01 11:00:00', '2017-01-01 14:00:00',
'2017-01-02 08:00:00', '2017-01-02 11:00:00', '2017-01-02 14:00:00',
'2017-01-03 08:00:00', '2017-01-03 11:00:00', '2017-01-03 14:00:00',
'2017-01-04 08:00:00'])).resample('1d').agg({'price': ['mean', 'max'], 'vol': ['mean', 'max']})
df.columns = ['price_mean', 'price_max', 'vol_mean', 'vol_max']
输出会像
price_mean price_max vol_mean vol_max
2017-01-01 72.666667 92 1692.333333 1853
2017-01-02 81.666667 94 1220.666667 1443
2017-01-03 61.000000 68 1466.000000 1684
2017-01-04 86.000000 86 1591.000000 1591