0

For dataframe df below, I would like to find the mean of the last 3 values in the sales column for each row, naming the result in a new column Mean_lst_3 as shown below, what's the best way to do it?

    STK_ID  RPT_Date  TClose   sales       Mean_lst_3
0   000568  20060331    3.69   5.975       NaN
1   000568  20060630    9.14  10.143       NaN
2   000568  20060930    9.49  13.854       NaN
3   000568  20061231   15.84  19.262       9.99 (= mean([5.975, 10.143, 13.854]))
4   000568  20070331   17.00   6.803       14.42(= mean([10.143, 13.854, 19.262]))
5   000568  20070630   26.31  12.940       13.31(= mean([13.854, 19.262, 6.803]))
6   000568  20070930   39.12  19.977       ...
7   000568  20071231   45.94  29.269       ...
8   000568  20080331   38.75  12.668       ...
9   000568  20080630   30.09  21.102       ...
10  000568  20080930   26.00  30.769       ...
cs95
  • 379,657
  • 97
  • 704
  • 746
nilsinelabore
  • 4,143
  • 17
  • 65
  • 122
  • Hi @cs95 I think you might have misunderstood my question, CMIIW, I was looking for the last 3 values based on the current row instead of the entire dataframe. – nilsinelabore Dec 21 '20 at 01:28
  • 1
    Ah, I got the wrong impression because of U11 forward's answer. Are you looking for `df['sales'].rolling(3).mean().shift()`? – cs95 Dec 21 '20 at 01:32

1 Answers1

0

Try tail:

print(df['sales'].tail(3))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114