I have a dataframe with datetime index that I am analysing. I have a column with prices.
Example
2022-01-01 00:00:00 | 32.21
2022-01-01 01:00:00 | 10.20
2022-01-01 02:00:00 | 42.12
2022-01-01 03:00:00 | 01.05
I am looking to make another column that lists how many hours it has been since the price was under a certain, constant value. With the above example, with values under 30, it would like:
2022-01-01 00:00:00 | 32.21 | 4
2022-01-01 01:00:00 | 10.20 | 0
2022-01-01 02:00:00 | 42.12 | 1
2022-01-01 03:00:00 | 01.05 | 0
How can I do this? I thought about putting the index and price into a list of lists or tuple, calculate, then put it back, but I assume there is a better way in Pandas?
Thanks,
Gregersdk
3条答案
按热度按时间j8yoct9x1#
不确定我是否正确理解了你想要什么,但这可能是你想要的:
它将根据价格与当前价格相比的时间长度添加新行,如果需要,您可以将'datetime.datetime.now()'更改为另一个任意值。
zte4gxcn2#
在data.csv中包含以下内容:
假设您 * 每小时有一个条目 *,您可以尝试以下操作:
结果应该是:
y1aodyip3#
如果我没理解错的话,这里有一个例子,说明如何使用for循环来完成这个任务。
输出量: