【发布时间】:2017-01-28 19:01:09
【问题描述】:
如何在 Datetime 索引的多级 Dataframe 中访问,例如:这是下载的 Fin 数据。 困难的部分是进入框架并访问特定内部级别的非相邻行,而无需明确指定外部级别日期,因为我有数千个这样的行..
ABC DEF GHI \
Date STATS
2012-07-19 00:00:00 NaN NaN NaN
investment 4 9 13
price 5 8 1
quantity 12 9 8
所以我正在搜索的 2 个公式可以总结为
X(today row) = quantity(prior row)*price(prior row)
or
X(today row) = quantity(prior row)*price(today)
困难在于如何使用 numpy 或 panda 为多级索引制定对这些行的访问,并且这些行不相邻。
最后我会得到这个:
ABC DEF GHI XN
Date STATS
2012-07-19 00:00:00 NaN NaN NaN
investment 4 9 13 X1
price 5 8 1
quantity 12 9 8
2012-07-18 00:00:00 NaN NaN NaN
investment 1 2 3 X2
price 2 3 4
quantity 18 6 7
X1= (18*2)+(6*3)+(7*4) (quantity_day_2 *price_day_2 data)
or for the other formula
X1= (18*5)+(6*8)+(7*1) (quantity_day_2 *price_day_1 data)
我可以使用 groupby 吗?
【问题讨论】:
-
你能用小整数值修改数据框(以便于验证)并从数据样本中添加所需的输出吗?谢谢。
-
哦,是的,可以的
-
请告诉我什么时候问题会被评论改变。谢谢。
-
@jezrael 好的。谢谢
-
一个问题 - 想要的输出是什么? 2 个新的
DataFrames?
标签: python pandas indexing dataframe multi-index