python-3.xpandasdataframerolling-sum

How can I create a new dataframe by taking the rolling COLUMN total/sum of another dataframe?


import pandas as pd
df = {'a': [1,1,1], 'b': [2,2,2], 'c': [3,3,3], 'd': [4,4,4], 'e': [5,5,5], 'f': [6,6,6], 'g': [7,7,7]}
df1 = pd.DataFrame(df, columns = ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
dg = {'h': [10,10,10], 'i': [14,14,14], 'j': [18,18,18], 'k': [22,22,22]}
df2 = pd.DataFrame(dg, columns = ['h', 'i', 'j', 'k'])

df1

    a   b   c   d   e   f   g
0   1   2   3   4   5   6   7
1   1   2   3   4   5   6   7
2   1   2   3   4   5   6   7

df1 is my original data frame. I would like to create another dataframe by adding each consecutive 4 columns (rolling column sum).

df2

    h   i   j   k
0   10  14  18  22
1   10  14  18  22
2   10  14  18  22

df2 is the resulting dataframe after adding 4 consecutive columns of df1.

For example: column h in df2 is the sum of columns a, b, c, d in df1; column i in df2 is the sum of columns b, c, d, e in df1; column j in df2 is the sum of columns c, d, e, f in df1; column k in df2 is the sum of columns d, e, f, g in df1.

I could not find any similar question/answer/example like this. I would appreciate any help.


Solution

  • You can use rolling over 4 columns and take the sum. Finally drop the first 3 columns.

    df1.rolling(4, axis=1).sum().dropna(axis=1)
    
        d       e       f       g
    0   10.0    14.0    18.0    22.0
    1   10.0    14.0    18.0    22.0
    2   10.0    14.0    18.0    22.0