0

I need to do a rolling sum of a columns of a dataframe, verify if the sum of the rolling (2 to 5 lines) is = 0 and take all the lines from the rolling to flag them as "canceled".

I tried this :

df_sorted = df.sort_values('No Piece FI')
df_sorted['No Piece FI'] = df_sorted['No Piece FI'].astype(str)
df = df_sorted[df_sorted['No Piece FI'].str.startswith('11')]
rolling_sum = df['CP'].rolling(window=5, min_periods=2).sum().shift(-4)
df.loc[(rolling_sum == 0), 'Annulé'] = 'Canceled'

But when I do that the result didn't seems to works. Also I would like to see the lines number like that or at least the group :

Annulé Lines CP Group (optionnal)
Not canceled 8
Not canceled 4
Canceled 1 10 1
Canceled 2 -10 1
Not canceled 3
Canceled 1 15 2
Canceled 2 -5 2
Canceled 3 -10 2
Canceled 1 8 3
Canceled 2 -8 3
Not canceled 19
Not canceled 3
Not canceled 2
Canceled 1 -8 4
Canceled 2 2 4
Canceled 3 2 4
Canceled 4 1 4
Canceled 5 3 4
Not canceled -1024

Here is what only the input would looks like :

CP
8
4
10
-10
15
-5
-10
8
-8
19
3
2
-8
2
2
1
3

How can I do that ? Thanks you.

Teddy
  • 143
  • 8

0 Answers0