2
votes

I have a data frame like this,

df
col1    col2
  A       1
  B       3
  C       2
  D       5
  E       6
  F       8
  G       10

I want to add previous and next n values of a particular value of col2 and store it into a new column,

So, If n=2, then the data frame should look like,

 col1    col2    col3
  A       1       6  (only below 2 values are there no upper values, so adding 3 numbers)
  B       3      11 (adding one prev, current and next two)
  C       2      17(adding all 4 values)
  D       5      24(same as above)
  E       6      31(same as above)
  F       8      29(adding two prev and next one as only one is present)
  G       10     24(adding with only prev two values)

When previous or next 2 values are not found adding whatever values are available. I can do it using a for loop, but the execution time will be huge, looking for some pandas shortcuts do do it most efficiently.

1
whats the formula to get col3 value please? I am unable to understand the logic for 5, 11, 17, 24.... Joe Ferndz
@JoeFerndz , 5=1+3+2 (as only 3 values are present) 11=1+3+2+5 17=1+3+2+5+6 24=3+2+5+6+8Kallol
@Kallol can you give mutliple examples for each column, including where it starts to decrease in column 3. Also, an you double-check the numbers. 5 does not equal 1 + 3 + 2 as in your first example.David Erickson
6 = (1 + 3 values (1 + 3 + 2)) = understood. 11 = 6 + 2 values (1 + 3). Shouldnt it be 10 instead of 11. Next 17 = 11 + 2 values (1 + 3) = Shouldnt it be 16? Is this what you are trying to do? I guess these numbers got mixed up coz you started with 5 and then changed first row to 6. Am I getting the logic right?Joe Ferndz
@DavidErickson explained the logic, check the edited oneKallol

1 Answers

5
votes

You can use the rolling method.

import pandas as pd
df = pd.read_json('{"col1":{"0":"A","1":"B","2":"C","3":"D","4":"E","5":"F","6":"G"},"col2":{"0":1,"1":3,"2":2,"3":5,"4":6,"5":8,"6":10}}')

df['col3'] = df['col2'].rolling(5, center=True, min_periods=0).sum()
col1    col2    col3
0   A   1   6.0
1   B   3   11.0
2   C   2   17.0
3   D   5   24.0
4   E   6   31.0
5   F   8   29.0
6   G   10  24.0