[英]Pandas: divide each row by another row depending on the index
I have a dataframe that contains the minute ticks of various securities from 930am-4pm every day. 我有一个数据框,其中包含每天930am-4pm的各种证券的分钟报价。 It looks like: 看起来像:
Date TUA COMDTY FVA COMDTY TYA COMDTY USA COMDTY \
0 2014-03-14 09:30:00 109.898438 119.523438 123.796875 131.34375
1 2014-03-14 09:31:00 109.898438 119.523438 123.796875 131.34375
2 2014-03-14 09:32:00 109.898438 119.523438 123.796875 131.34375
3 2014-03-14 09:33:00 109.898438 119.523438 123.796875 131.34375
4 2014-03-14 09:34:00 109.898438 119.523438 123.796875 131.34375
5 2014-03-14 09:35:00 109.898438 119.523438 123.796875 131.34375
6 2014-03-14 09:36:00 109.898438 119.523438 123.796875 131.34375
7 2014-03-14 09:37:00 109.898438 119.523438 123.796875 131.34375
8 2014-03-14 09:38:00 109.898438 119.523438 123.796875 131.34375
9 2014-03-14 09:39:00 109.898438 119.523438 123.796875 131.34375
10 2014-03-14 09:40:00 109.898438 119.523438 123.796875 131.34375
11 2014-03-14 09:41:00 109.898438 119.523438 123.796875 131.34375
12 2014-03-14 09:42:00 109.898438 119.523438 123.796875 131.34375
13 2014-03-14 09:43:00 109.898438 119.523438 123.796875 131.34375
14 2014-03-14 09:44:00 109.898438 119.523438 123.796875 131.34375
15 2014-03-14 09:45:00 109.898438 119.523438 123.796875 131.34375
16 2014-03-14 09:46:00 109.898438 119.523438 123.796875 131.34375
17 2014-03-14 09:47:00 109.898438 119.523438 123.796875 131.34375
18 2014-03-14 09:48:00 109.898438 119.523438 123.796875 131.34375
19 2014-03-14 09:49:00 109.898438 119.523438 123.796875 131.34375
20 2014-03-14 09:50:00 109.898438 119.523438 123.796875 131.34375
21 2014-03-14 09:51:00 109.898438 119.523438 123.796875 131.34375
22 2014-03-14 09:52:00 109.898438 119.523438 123.796875 131.34375
23 2014-03-14 09:53:00 109.898438 119.523438 123.796875 131.34375
24 2014-03-14 09:54:00 109.898438 119.523438 123.796875 131.34375
25 2014-03-14 09:55:00 109.898438 119.523438 123.796875 131.34375
26 2014-03-14 09:56:00 109.898438 119.523438 123.796875 131.34375
27 2014-03-14 09:57:00 109.898438 119.523438 123.796875 131.34375
28 2014-03-14 09:58:00 109.898438 119.523438 123.796875 131.34375
29 2014-03-14 09:59:00 109.898438 119.523438 123.796875 131.34375
30 2014-03-14 10:00:00 109.898438 119.523438 123.796875 131.34375
31 2014-03-14 10:01:00 109.898438 119.523438 123.796875 131.34375
32 2014-03-14 10:02:00 109.898438 119.523438 123.796875 131.34375
33 2014-03-14 10:03:00 109.898438 119.523438 123.796875 131.34375
34 2014-03-14 10:04:00 109.898438 119.523438 123.796875 131.34375
35 2014-03-14 10:05:00 109.898438 119.523438 123.796875 131.34375
36 2014-03-14 10:06:00 109.898438 119.523438 123.796875 131.34375
37 2014-03-14 10:07:00 109.898438 119.523438 123.796875 131.34375
38 2014-03-14 10:08:00 109.898438 119.523438 123.796875 131.34375
39 2014-03-14 10:09:00 109.898438 119.523438 123.796875 131.34375
40 2014-03-14 10:10:00 109.898438 119.523438 123.796875 131.34375
41 2014-03-14 10:11:00 109.898438 119.523438 123.796875 131.34375
42 2014-03-14 10:12:00 109.898438 119.523438 123.796875 131.34375
43 2014-03-14 10:13:00 109.898438 119.523438 123.796875 131.34375
44 2014-03-14 10:14:00 109.898438 119.523438 123.796875 131.34375
45 2014-03-14 10:15:00 109.898438 119.523438 123.796875 131.34375
46 2014-03-14 10:16:00 109.898438 119.523438 123.796875 131.34375
47 2014-03-14 10:17:00 109.898438 119.523438 123.796875 131.34375
48 2014-03-14 10:18:00 109.898438 119.523438 123.796875 131.34375
49 2014-03-14 10:19:00 109.898438 119.523438 123.796875 131.34375
50 2014-03-14 10:20:00 109.898438 119.523438 123.796875 131.34375
51 2014-03-14 10:21:00 109.898438 119.523438 123.796875 131.34375
52 2014-03-14 10:22:00 109.898438 119.523438 123.796875 131.34375
53 2014-03-14 10:23:00 109.898438 119.523438 123.796875 131.34375
54 2014-03-14 10:24:00 109.898438 119.523438 123.796875 131.34375
55 2014-03-14 10:25:00 109.898438 119.523438 123.796875 131.34375
56 2014-03-14 10:26:00 109.898438 119.523438 123.796875 131.34375
57 2014-03-14 10:27:00 109.898438 119.523438 123.796875 131.34375
58 2014-03-14 10:28:00 109.898438 119.523438 123.796875 131.34375
59 2014-03-14 10:29:00 109.898438 119.523438 123.796875 131.34375
... ... ... ... ...
I'm trying to get a dataframe that reflects the price change from the previous day. 我正在尝试获取一个反映前一天价格变化的数据框。 Implemented as a loop, for each row, I would get the prices at 4pm from the previous day to divide the prices in the current row. 作为一个循环实现,对于每一行,我将获得前一天下午4点的价格,以将当前行中的价格进行划分。 Right now I'm doing resampling at 4pm every day, match index with the original matrix with forward filling. 现在,我每天下午4点进行重采样,将索引与原始矩阵进行匹配并向前填充。 The new dataframe has the same dimension as the original, and contains the prices I need to divide with on each row. 新的数据框具有与原始数据框相同的尺寸,并包含我需要在每一行上除以的价格。 I then divide the two matrices. 然后,我将两个矩阵相除。 Is there an easier way to do this, using apply() for example? 有没有更简单的方法可以做到这一点,例如使用apply()? The function to be applied uses something like a vlookup in excel. 要应用的功能使用excel中的vlookup之类的东西。 The problem is I need the index (date) for each row, which is not available in apply(). 问题是我需要每一行的索引(日期),在apply()中不可用。 Any thoughts? 有什么想法吗? Thank you! 谢谢!
I assume you are just doing return since close. 我假设您自关闭以来就在做回报。
I have a hack for this purpose. 我为此有一个技巧。 I resample the array to daily data and join it to my original frame. 我将阵列重新采样为每日数据,然后将其加入我的原始框架。 Then I fill the closing price forward. 然后我将收盘价向前补齐。 How about something like this? 这样的事情怎么样?
df_daily = df[df["Date"].hour == 16].copy().shift(1)
df = df.join(df_daily, rsuffix="closing_").fillna(method="ffill")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.