Answer the question
In order to leave comments, you need to log in
How to create a list that contains a consecutive number of repetitions of an element in Python?
There is the following sequence
import pandas as pd
a = pd.DataFrame({'index':range(13),'a':[1,1,1,2,2,2,1,1,3,3,4,3,5]})
index a
0 1
1 1
2 1
3 2
4 2
5 2
6 1
7 1
8 3
9 3
10 4
11 3
12 5
index a Range
0 1 1
1 1 2
2 1 3
3 2 1
4 2 2
5 2 3
6 1 1
7 1 2
8 3 1
9 3 2
10 4 1
11 3 1
12 5 1
group = a.groupby(["a"])
a = a.assign(Range = a-group.transform(min)+1)
index a Range
0 1 1
1 1 2
2 1 3
3 2 1
4 2 2
5 2 3
6 1 7
7 1 8
8 3 1
9 3 2
10 4 1
11 3 4
12 5 1
Answer the question
In order to leave comments, you need to log in
I finally solved it this way:
a['Range'] = a.groupby((aa != aashift()).cumsum()).cumcount() + 1
Try to adapt as an option (or maybe there is something ready for such cases in numpy , but I don’t remember offhand):
>>> x = [1, 1, 1, 2, 2, 2, 1, 1, 3, 3, 4, 3, 5]
>>> from itertools import groupby
>>> x_groups_count = [num for _, group in groupby(x) for num, _ in enumerate(group, 1)]
>>> x_groups_count
[1, 2, 3, 1, 2, 3, 1, 2, 1, 2, 1, 1, 1]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question