【发布时间】:2017-08-31 21:02:42
【问题描述】:
我有一个 df pandas,列中只有一个“价格”和索引日期。我想在里面找到一个名为“Aprox”的新列
aprox. = price of today - price of one year ago (or closest date from a year ago) -
price in one year (again take aprox if exact one year price don't exist)
for example
aprox. 2019-04-30 = 8 -4 -10 = -6 = aprox. 2019-04-30
- aprox. 2018-01-31 - aprox.2020-07-30
说实话,我有点挣扎......
ex. [in]: Price
2018-01-31 4
2019-04-30 8
2020-07-30 10
2020-10-31 9
2021-01-31 14
2021-04-30 150
2021-07-30 20
2022-10-31 14
[out]: Price aprox.
2018-01-31 4
2019-04-30 8 -6 ((8-4-10) = -6) since there is no 2018-04-30
2020-07-30 10 -12 (10-14-8)
2020-10-31 9 ...
2021-01-31 14 ...
2021-04-30 150
2021-07-30 20
2022-10-31 14
我对此非常苦恼......甚至更多的是大约。
非常感谢!!
【问题讨论】:
标签: python python-2.7 date pandas dataframe