R
R
rudolfxcp2018-06-13 15:46:24
Python
rudolfxcp, 2018-06-13 15:46:24

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

You need to count the number of repetitions of each element and add a column, for example Range:
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

I would like to do this without loops, otherwise I have too many calculations, as a result, a very long execution is obtained. I made this code:
group = a.groupby(["a"])
a = a.assign(Range = a-group.transform(min)+1)

But when the same values ​​hit (for example, in column a there are three units in a row, and then they are repeated in id: 6,7), it counts from the place where the previous units ended, i.e. this will be 7.8 units in the list:
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

2 answer(s)
R
rudolfxcp, 2018-06-13
@rudolfxcp

I finally solved it this way:
a['Range'] = a.groupby((aa != aashift()).cumsum()).cumcount() + 1

A
aRegius, 2018-06-13
@aRegius

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 question

Ask a Question

731 491 924 answers to any question