How can I split a Pandas dataframe into multiple dataframes based on the value in a column?
df = pd.DataFrame({'A':[4,5,0,0,5,0,0,4],
'B':[7,8,0,0,4,0,0,0],
'C':[1,3,0,0,7,0,0,0]}, columns = ['A','B','C'])
df["sum"] = df.sum(axis=1)
df["Rolling_sum"] = df["sum"].rolling(2, min_periods=1).sum()
The resulting dataframe is:
A B C sum Rolling_sum
0 4 7 1 12 12.0
1 5 8 3 16 28.0
2 0 0 0 0 16.0
3 0 0 0 0 0.0
4 5 4 7 16 16.0
5 0 0 0 0 16.0
6 0 0 0 0 0.0
7 4 0 0 4 4.0
I want to split the dataframe into multiple dataframe based on the occurrence of 0 in the Rolling_sum column.
Expected result:
Dataframe 1:
A B C sum Rolling_sum
0 4 7 1 12 12.0
1 5 8 3 16 28.0
2 0 0 0 0 16.0
Dataframe 2:
A B C sum Rolling_sum
4 5 4 7 16 16.0
5 0 0 0 0 16.0
Dataframe 3:
A B C sum Rolling_sum
7 4 0 0 4 4.0
I'm not sure what condition(s) I can use to split the dataframe.