4

Is there a way to create a rolling window (2 periods) over a dataframe rows and compute the sum of the values?

My data:

ID Name Value1 Value2 Value3 Value4
0   A      2       2      4      4
1   B      1       1      3      3

The output desired:

ID Name Value1 Value2 Value3 Value4   Rol1 Rol2 Rol3
0   A      2       2      4      4       4    6    8
1   B      1       1      3      3       2    4    6

I tried to use df.rolling() but was only able to use it on a specific column

1 Answer 1

4

You can use rolling, but first set_index of all columns which cannot be used in function and last add dropna for remove all NaNs columns:

df1 = (df.set_index(['ID','Name'])
       .rolling(2, axis=1).sum()
       .dropna(axis=1, how='all'))
#rename columns
df1.columns = ['Roll{}'.format(x) for x in range(1, len(df1.columns)+1)]
print (df1)
         Roll1  Roll2  Roll3
ID Name                     
0  A       4.0    6.0    8.0
1  B       2.0    4.0    6.0

Last join output to original:

df = df.join(df1, on=['ID','Name'])
print (df)
   ID Name  Value1  Value2  Value3  Value4  Roll1  Roll2  Roll3
0   0    A       2       2       4       4    4.0    6.0    8.0
1   1    B       1       1       3       3    2.0    4.0    6.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.