- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 df(样本最后粘贴在这里)。我正在寻找每个过去 10 分钟或任何其他滚动周期中哪个 tradePrice
具有最多的 tradeVolume
。
这是根据附带的示例数据在 xls 中完成的数据透视表。
minute
tradePrice Data 0 1 10 Total Result
12548 Sum - tradeVolume 3 3
Count - tradePrice 2 2
12548.5 Sum - tradeVolume 1 1
Count - tradePrice 1 1
12549 Sum - tradeVolume 1 1
Count - tradePrice 1 1
12549.5 Sum - tradeVolume 95 95
Count - tradePrice 5 5
12550 Sum - tradeVolume 6 6
Count - tradePrice 4 4
12559 Sum - tradeVolume 93 93
Count - tradePrice 1 1
12559.5 Sum - tradeVolume 1 1
Count - tradePrice 1 1
12560 Sum - tradeVolume 5 5
Count - tradePrice 4 4
12560.5 Sum - tradeVolume 3 5 8
Count - tradePrice 3 2 5
12561 Sum - tradeVolume 4 5 9
Count - tradePrice 2 3 5
12561.5 Sum - tradeVolume 3 2 5
Count - tradePrice 3 1 4
12562 Sum - tradeVolume 9 7 16
Count - tradePrice 8 1 9
12562.5 Sum - tradeVolume 6 2 8
Count - tradePrice 2 2 4
12563 Sum - tradeVolume 2 2
Count - tradePrice 1 1
Total Sum - tradeVolume 120 27 106 253
Total Count - tradePrice 20 14 13 47
结果需要是一个像这样搜索交易量最大的价格的 df):
Price Volume
02:00:00 AM 12559 93
02:01:00 AM 12562 7
02:10:00 AM 12549.5 95
为了获得 1 分钟。结果我分组并应用了以下功能
def f(x): # function to find the POC price and volume
a = x['tradePrice'].value_counts().index[0]
b = x.loc[x['tradePrice'] == a, 'tradeVolume'].sum()
return pd.Series([a, b], ['POC_Price', 'POC_Volume'])
groupbytime = (str(Time)+"min")#ther is a column name by this
groups = df.groupby(groupbytime,as_index=True)
df_POC = groups.apply(f) #applys the function of the POC on the grouped data
我的问题是:我怎样才能得到相同的解决方案,但每个滚动时间段(不能少于 1 分钟)所以最后 10 分钟的预期结果(以最大交易量交易的价格)是:
Price Volume
02:10:00 AM 12549.5 95
提前致谢!
示例数据:
dateTime tradePrice tradeVolume 1min time_of_day_10 time_of_day_30 date hour minute
0 2017-09-19 02:00:04 12559 93 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
49 2017-09-19 02:00:11 12562 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
50 2017-09-19 02:00:12 12563 2 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
51 2017-09-19 02:00:12 12562 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
122 2017-09-19 02:00:34 12561.5 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
123 2017-09-19 02:00:34 12562 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
127 2017-09-19 02:00:34 12562 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
129 2017-09-19 02:00:35 12561 2 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
130 2017-09-19 02:00:35 12560.5 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
131 2017-09-19 02:00:35 12561.5 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
135 2017-09-19 02:00:39 12562 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
136 2017-09-19 02:00:39 12562 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
137 2017-09-19 02:00:43 12561.5 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
138 2017-09-19 02:00:43 12561 2 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
139 2017-09-19 02:00:43 12560.5 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
140 2017-09-19 02:00:43 12560.5 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
152 2017-09-19 02:00:45 12562 2 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
153 2017-09-19 02:00:46 12562.5 4 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
166 2017-09-19 02:00:58 12562 1 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
167 2017-09-19 02:00:58 12562.5 2 2017-09-19 02:00:00 02:00:00 02:00:00 2017-09-19 2 0
168 2017-09-19 02:01:00 12562 7 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
169 2017-09-19 02:01:00 12562.5 1 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
170 2017-09-19 02:01:00 12562.5 1 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
171 2017-09-19 02:01:00 12561.5 2 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
175 2017-09-19 02:01:03 12561 1 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
176 2017-09-19 02:01:03 12561 3 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
187 2017-09-19 02:01:07 12560.5 2 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
188 2017-09-19 02:01:08 12561 1 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
189 2017-09-19 02:01:10 12560 1 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
190 2017-09-19 02:01:10 12560 1 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
191 2017-09-19 02:01:10 12559.5 1 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
192 2017-09-19 02:01:11 12560 1 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
193 2017-09-19 02:01:12 12560 2 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
194 2017-09-19 02:01:12 12560.5 3 2017-09-19 02:01:00 02:00:00 02:00:00 2017-09-19 2 1
593 2017-09-19 02:10:00 12550 1 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
594 2017-09-19 02:10:00 12549.5 12 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
604 2017-09-19 02:10:12 12548.5 1 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
605 2017-09-19 02:10:15 12549.5 22 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
606 2017-09-19 02:10:16 12549.5 21 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
636 2017-09-19 02:10:45 12548 1 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
637 2017-09-19 02:10:47 12548 2 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
638 2017-09-19 02:10:47 12549.5 23 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
639 2017-09-19 02:10:48 12549.5 17 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
640 2017-09-19 02:10:49 12549 1 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
665 2017-09-19 02:10:58 12550 1 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
666 2017-09-19 02:10:58 12550 1 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
667 2017-09-19 02:10:58 12550 3 2017-09-19 02:10:00 02:10:00 02:00:00 2017-09-19 2 10
最佳答案
如果我正确理解您的问题,您需要选择时间粒度和时间窗口。然后,您可以结合使用 groupby + unstack + rolling 来完成此操作。
首先是groupby:
time_grain = '1min'
df = df.groupby([pd.Grouper(key='dateTime', freq=time_grain),'tradePrice']).tradeVolume.sum()
dateTime tradePrice
2017-09-19 02:00:00 12559.0 93
12560.5 3
12561.0 4
12561.5 3
12562.0 9
12562.5 6
12563.0 2
2017-09-19 02:01:00 12559.5 1
12560.0 5
12560.5 5
12561.0 5
12561.5 2
12562.0 7
12562.5 2
2017-09-19 02:10:00 12548.0 3
12548.5 1
12549.0 1
12549.5 95
12550.0 6
Name: tradeVolume, dtype: int64
然后展开+滚动:
window_size = '10min'
df = df.unstack('tradePrice').rolling(window_size).sum()
tradePrice 12548.0 12548.5 12549.0 12549.5 12550.0 12559.0 \
dateTime
2017-09-19 02:00:00 NaN NaN NaN NaN NaN 93.0
2017-09-19 02:01:00 NaN NaN NaN NaN NaN 93.0
2017-09-19 02:10:00 3.0 1.0 1.0 95.0 6.0 NaN
tradePrice 12559.5 12560.0 12560.5 12561.0 12561.5 12562.0 \
dateTime
2017-09-19 02:00:00 NaN NaN 3.0 4.0 3.0 9.0
2017-09-19 02:01:00 1.0 5.0 8.0 9.0 5.0 16.0
2017-09-19 02:10:00 1.0 5.0 5.0 5.0 2.0 7.0
tradePrice 12562.5 12563.0
dateTime
2017-09-19 02:00:00 6.0 2.0
2017-09-19 02:01:00 8.0 2.0
2017-09-19 02:10:00 2.0 NaN
最后将 tradePrice 堆叠回索引并找到每个时间段具有最高值的索引:
df = df.stack('tradePrice')
idx_list = df.groupby('dateTime').idxmax()
result = df.loc[idx_list]
dateTime tradePrice
2017-09-19 02:00:00 12559.0 93.0
2017-09-19 02:01:00 12559.0 93.0
2017-09-19 02:10:00 12549.5 95.0
dtype: float64
请注意,如果您将滚动与时间偏移一起使用,则滚动默认的最小观察次数为 1。这就是为什么您会得到 3 个结果行。
我认为这种方法的最大缺点是对于具有大量价格点的大数据框,这将占用大量内存(因为每个价格点都会生成一个新列)。
关于python - python中的滚动数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49451313/
我正在尝试使用透视投影描绘一个立方体,但我得到的只是一个正方形的角。正方形的面设置在原点并向正方向扩展。使用 glOrtho 我可以设置坐标系,但我在使用 glPerspective 做同样的事情时遇
SELECT j.departure, stopDepartures.* FROM journey j JOIN journey_day ON journey_day.journey = j.id J
我确实需要一些帮助来了解如何根据相似的值对表格进行透视。 day | startDate ----------------------- Monday | 09:00 Monday |
我有以下数据框 df = pd.DataFrame({ '1': ['Mon (07/08)','Sales', '2'], '2': ['Mon (07/0
dummy_df = pd.DataFrame({ 'accnt' : [101, 102, 103, 104, 101, 102, 103, 104, 101, 102, 103, 104,
public class MainActivity extends Activity { LinearLayout rotator; protected void onCreate(Bundle sa
我正在尝试通过 PHP 更改 ImageMagick 中 Plane2Cylinder 失真的视角。 为了帮助解释我在寻找什么,我制作了这张图: 您可以看到红色 block 的下部比顶部的半径更大,就
我有一个像这样的简单查询.. USE AdventureWorks; GO SELECT DaysToManufacture, AVG(StandardCost) AS AverageCost FRO
我希望我可以更改架构,但我受制于它,假设我有以下表格 JanDataTable FebDataTable MarDataTable ProductsTable 其中前三个表有 ID 和 Amount
我正在将我们的一个旧应用程序从 vb6 更新到 c#,在此过程中必须重新创建原始程序员设计的自定义控件。该控件简单地获取对象的尺寸,矩形或圆锥形,并在 3D 中放置对象的轮廓草图(我认为在技术上是 2
我一直在尝试在 MySQL 中对表进行透视(将行移动到列)。我知道 mysql 没有枢纽功能,所以我认为需要联合,但不是 100% 确定。我有三列,user_id、option_id 和 Questi
我正在尝试旋转像这样创建的 mysql 表 'CREATE TABLE `fundreturns` ( `Timestamp` datetime NOT NULL, `FundName` varcha
提前感谢任何对此提供帮助的人。我知道我以前做过这件事,没有太多痛苦,但似乎找不到解决方案 我的数据库看起来像这样: `tbl_user: ---------- id ( pkey )
我正在尝试开发 X 轴方向的卡片翻转动画。截至目前,div 现在只需使用 rotateX() 方法进行旋转。我试过对上层 div 使用透视属性,而不是工作它扭曲了我的 div 结构。因为,这只是一个工
我有一个带有 CSS3 透视图的 DIV 元素。 DIV 包含 2 个子 DIV,其中之一在 z 轴上有平移。这应该会导致一个 DIV 在另一个前面,因此后面的那个应该被挡住。 然而,这些 DIV 的
大家好,我有一张这样的 map http://sinanisler.com/demo/map/ 如您所见,有一些树,但不是真正的视角,我想要这个 http://sinanisler.com/demo/
我有以下代码将快照拍摄到帧缓冲区。我验证了帧缓冲区工作正常并且相机正确地面向对象。我曾经正确地完成图片,但它是基于错误的代码,使用了错误的截锥体。所以我决定重新开始(使用截锥体)。 物体以中间为中心,
我正在尝试将求和列添加到透视数据框,但不断收到数据解析错误。 mydata = [{'amount': 3200, 'close_date':'2013-03-31', 'customer': 'Cu
我正在尝试将一些 groupby/crosstabbing 逻辑应用于用户定义对象的 IEnumerable 列表,并且想知道是否有人可以帮助我。我坚持使用现有的(相当烦人的)对象模型来工作,但无论如
我想使用一个 CALayer 创建如下图所示的效果 - 而不是通过拆分图像、对两半进行透视变换然后将它们并排放置。 可以使用 CoreImage 以任何方式完成吗? 或者,有人可以使用 OpenGL
我是一名优秀的程序员,十分优秀!