0

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.

1 Answer 1

2

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()
2
  • Exactly what I was looking for. Would you please explain the synthax? Thank you! Commented Jul 10 at 7:29
  • 1
    There isn't much more than what you already had, just grabbing the values as a np.array with .values then flatening to 1d array with .flatten(), put that back into a pd.DataFrame(...), aplly the same rolling logic as yours, finally grab the min value with .min().
    – Julien
    Commented Jul 10 at 7:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.