我有一个如下所示的 csv 文件:
date price volume
2017-10-17 01:00:11.031 51.91 1
2017-10-17 01:00:11.828 51.91 1
2017-10-17 01:00:12.640 51.91 1
2017-10-17 01:00:13.140 51.90 -9
2017-10-17 01:00:15.328 51.90 -5
2017-10-17 01:00:16.531 51.90 1
2017-10-17 01:00:16.531 51.89 -2
2017-10-17 01:00:19.937 51.90 1
2017-10-17 01:00:24.546 51.90 1
2017-10-17 01:00:25.250 51.90 1
2017-10-17 01:00:32.843 51.89 -9
2017-10-17 01:00:42.859 51.89 -5
2017-10-17 01:00:43.453 51.89 -1
2017-10-17 01:00:43.546 51.90 1
2017-10-17 01:00:45.953 51.90 7
...
我想制作一个数据框,显示每个价格水平每 5 分钟累积的交易量。
例如,如果 2017-10-17 00:00 ~ 2017-10-17 00:05 之间的最高价和最低价分别为 51.21 和 51.11,则结果为:
datetime price pos_volume neg_volume
2017-10-17 00:00 51.21 3 4
51.20 21 23
51.19 44 21
51.18 31 33
...
51.14 14 21
51.13 30 29
51.12 2 3
51.11 5 1
有两列用于区分正体积和负体积。
我想如果我使用很多条件循环我就可以做到这一点,但我想知道是否有更多Pythonic的简单方法可以做到这一点。感谢您阅读本文!
最佳答案
您可以使用np.where
分隔正值和负值,然后使用索引为grouper
的数据透视表,频率为5分钟,然后使用aggfunc
作为 count
(它忽略 nan 值)。
df['pos_vol'] = np.where(df['volume']>0,df['volume'],np.nan)
df['neg_vol'] = np.where(df['volume']<0,df['volume'],np.nan)
ndf = df.pivot_table(values=['pos_vol','neg_vol'],index=[pd.Grouper(key='date', freq='5min'),'price'],aggfunc='count')
输出:
neg_vol pos_vol date price 2017-10-17 01:00:00 51.89 4 0 51.90 2 6 51.91 0 3
对于排序索引,您可以使用ndf = ndf.sort_index(level=1,ascending=False)
输出:
neg_vol pos_vol date price 2017-10-17 01:00:00 51.91 0 3 51.90 2 6 51.89 4 0
关于python - 如何重新组合数据框并累积列的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842320/