2017-04-06 75 views
0

我有一個df pandas,列中只有'Price'和索引日期。我想找到一個名爲「APROX」新列內從Python/Pandas中查找近似值的一年內日期

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 

說實話我與有些strugling ...

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 

我strugling非常與...甚至更多與約。

非常感謝!

回答

1

這不是很清楚,我是你正在嘗試做的,但也許這是你想要什麼:

import pandas 

def last_year(x): 
    """ 
    Return date from a year ago. 
    """ 
    return x - pandas.DateOffset(years=1) 

# Simulate the data you provided in example 
dt_str = ['2018-01-31', '2019-04-30', '2020-07-30', '2020-10-31', 
      '2021-01-31', '2021-04-30', '2021-07-30', '2022-10-31'] 
dates = [pandas.Timestamp(x) for x in dt_str] 
df = pandas.DataFrame([4, 8, 10, 9, 14, 150, 20, 14], columns=['Price'], index=dates) 

# This is the code that does the work 
for dt, value in df['Price'].iteritems(): 
    df.loc[dt, 'approx'] = value - df['Price'].asof(last_year(dt)) 

這給了我下面的結果:

In [147]: df 
Out[147]: 
       Price approx 
2018-01-31  4  NaN 
2019-04-30  8  4.0 
2020-07-30  10  2.0 
2020-10-31  9  1.0 
2021-01-31  14  6.0 
2021-04-30 150 142.0 
2021-07-30  20 10.0 
2022-10-31  14 -6.0 

底線是對於這種類型的操作,您不能只使用apply操作,因爲您需要索引和值。

+0

嗨,謝謝你的回覆@ aquil.abdullah!這不是我正在尋找的。例如,對於2019-04-30,「約」欄應回報2019-04-30(此處爲8)的價格,減去一年前的價格減去一年後的價格。如果在我的數據集之前或之後沒有確切的一年的價格,我只想從這一年中選擇最接近的價格/日期,所以這裏是2020-07-30和2018-01-31的價格。 微積分應該是8 - 4 - 10 = - 6 – user6457870

相關問題