pythonrolling-sum

Rolling Sum of dataframe columns


I have a dataframe with following values:

df = pd.DataFrame({
                  'S1':[38.868, 39.231, 39.427, 38.992, 39.071, 39.003],
                  'S2':[63.631, 63.239, 63.463, 63.675, 63.571, 63.692],
                  'S3':[35.406, 35.483, 35.107, 35.346, 35.413, 35.362]})

I would like to get the minimum rolling sum.

What I mean is that I am looking to calculate the sum of 3 consecutive numbers - for example 38.868 + 63.631 + 35.046, then 63.631 + 35.406 + 39.231, then 35.406 + 39.231 + 63.239, etc, etc, — and then get the min number.

I was trying it by using:

df.rolling(window=3, min_periods=3).sum()

But I do not get the desired result.


Solution

  • Looks like you don't care about the 'Si' column structure, so you first need to flatten your 2d array:

    pd.DataFrame(df.values.flatten()).rolling(window=3, min_periods=3).sum().min()