Reputation: 352
I want to group and agg a column to one list, after group I will get :
col
0
1
1
1
0
0
0
1
0
And I want to make a list after agg: ['0-1','1-3','0-3','1-1','0-1']
, like <label>-<count>
Upvotes: 1
Views: 57
Reputation: 153510
Try this:
d = {0: 0, 1: 1, 2: 1, 3: 1, 4: 0, 5: 0, 6: 0, 7: 1, 8: 0}
s = pd.Series(d)
g = (s != s.shift()).cumsum()
s.groupby(g).agg(lambda x: f'{x.iloc[0]}-{x.count()}')
Output:
col
1 0-1
2 1-3
3 0-3
4 1-1
5 0-1
Name: col, dtype: object
Upvotes: 3