pyCthon
pyCthon

Reputation: 12341

Pandas group by using values in a Column

I would like to be able to group the values in my DataFrame by a separation value such that everything in between is grouped together. In my case I would like Status == Finished to separate the grouping. I am not sure how to do this with pandas, Example:

I have a data frame as such:

df = pd.DataFrame({'Status':['Pending', 'Pending', 'Finished', 'Finished', 'Pending', 'Finished'], "Value":[1, 2, 3, 4, 5, 6]})

     Status  Value
0   Pending      1
1   Pending      2
2  Finished      3
3  Finished      4
4   Pending      5
5  Finished      6

I would like to this to become 3 Separate DataFrames:

     Status  Value
0   Pending      1
1   Pending      2
2  Finished      3

     Status  Value
3  Finished      4

     Status  Value
4   Pending      5
5  Finished      6

Upvotes: 1

Views: 76

Answers (1)

BENY
BENY

Reputation: 323226

Let us try

d = dict(tuple(df.groupby(df.Status.eq('Finished').iloc[::-1].cumsum())))
d[1]
Out[229]: 
     Status  Value
4   Pending      5
5  Finished      6
d[2]
Out[230]: 
     Status  Value
3  Finished      4

Upvotes: 2

Related Questions