我有一個每週五包含5年數據的週報利潤dataframe
。熊貓:通過現有列之間的線性插值創建新列
數據框的樣子:
更新的數據:
Date Profit
7/28/2017 2,923
7/21/2017 2,879
7/14/2017 2,832
7/7/2017 2,773
6/30/2017 2,701
6/23/2017 2,635
6/16/2017 2,563
6/9/2017 2,481
6/2/2017 2,394
:
7/29/2016 2,824
7/22/2016 2,770
7/15/2016 2,718
7/8/2016 2,657
7/1/2016 2,580
6/24/2016 2,503
6/17/2016 2,425
6/10/2016 2,337
6/3/2016 2,250
:
7/31/2015 2,848
7/24/2015 2,796
7/17/2015 2,748
7/10/2015 2,694
7/3/2015 2,623
6/26/2015 2,548
6/19/2015 2,474
6/12/2015 2,387
6/5/2015 2,301
:
我想interpolate
利潤在過去幾年同日而不論平日爲日期,並填寫下面的第3和第4列。
week_ending profit Profit one_yr_ago Profilt 2_yrs_ago
0 2017-07-28 3,010 (Profit on 2016-07-28) (profit on 2015-07-28)
1 2017-07-21 2,990 (Profit on 2016-07-21) (profit on 2015-07-21)
2 2017-07-14 2,973 -- --
3 2017-07-07 2,945 -- --
4 2017-06-30 2,888
5 2017-06-23 2,816
6 2017-06-16 2,770
7 2017-06-09 2,709
8 2017-06-02 2,631
9 2017-05-26 2,525
我已經使用上年np.interp
和x[0] - pd.DateOffset(years=1)
,但不知道如何得到這個工作嘗試。我應該使用rolling_apply來有效地做到這一點嗎?
編輯 @JohanL和@Cheryl:數據是每週五捕獲的。例如:我們知道2017-07-28是星期五,但2016-07-28不是星期五。在新的專欄中,我想插入去年和兩年前同一日期的利潤。我已經更新了實際的數據幀,以表示6月和7月份的3年數據。
它是什麼,你要插找?利用當年的可用日期,一年(或多年)前一天的利潤?這是相當不清楚的。 – JohanL