Batmax
Batmax

Reputation: 253

Rolling window on dataframe rows Python 3

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

Upvotes: 4

Views: 858

Answers (1)

jezrael
jezrael

Reputation: 862431

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

Upvotes: 4

Related Questions