熊猫:根据索引将每行除以另一行

时间:2014-07-18 13:42:35

标签: python pandas

我有一个数据框,其中包含每天上午9:30至下午4:00各种证券的分钟刻度。它看起来像:

                  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   
                   ...         ...         ...         ...         ...   

我试图获得一个反映前一天价格变化的数据框。作为一个循环实现,对于每一行,我会在前一天的下午4点得到价格,以划分当前行中的价格。 现在我每天下午4点进行重新采样,将索引与原始矩阵匹配,并进行前向填充。新数据框与原始数据框具有相同的维度,并包含我需要在每行上划分的价格。然后我划分了两个矩阵。 有没有更简单的方法来执行此操作,例如使用apply()?要应用的函数在excel中使用类似vlookup的功能。问题是我需要每行的索引(日期),这在apply()中是不可用的。 有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我假设你只是在关闭后才回来。

我有一个黑客为此目的。我将数组重新采样为每日数据并将其连接到我的原始帧。然后我填写收盘价。  这样的事情怎么样?

df_daily = df[df["Date"].hour == 16].copy().shift(1)
df = df.join(df_daily, rsuffix="closing_").fillna(method="ffill")