Aleksandr K.
Aleksandr K.

Reputation: 1415

How to select ranges of values in pandas?

Newbie question.

My dataframe looks like this:

      class   A         B
0         1  3.767809   11.016
1         1  2.808231    4.500
2         1  4.822522    1.008
3         2  5.016933   -3.636
4         2  6.036203   -5.220
5         2  7.234567   -6.696
6         2  5.855065   -7.272
7         4  4.116770   -8.208
8         4  2.628000  -10.296
9         4  1.539184  -10.728
10        3  0.875918  -10.116
11        3  0.569210   -9.072
12        3  0.676379   -7.632
13        3  0.933921   -5.436
14        3  0.113842   -3.276
15        3  0.367129   -2.196
16        1  0.968661   -1.980
17        1  0.160997   -2.736
18        1  0.469383   -2.232
19        1  0.410463   -2.340
20        1  0.660872   -2.484

I would like to get groups where class is the same, like:

class 1: rows 0..2
class 2: rows 3..6
class 4: rows 7..9
class 3: rows 10..15
class 1: rows 16..20

The reason is that order matters. In requirements I have that class 4 can be only between 1 and 2 and if after prediction we have class 4 after 2 it should be considered as 2.

Upvotes: 0

Views: 37

Answers (1)

BENY
BENY

Reputation: 323386

Build a New Para to identify the group

df['group']=df['class'].diff().ne(0).cumsum()

df.groupby('group')['group'].apply(lambda x : x.index)
Out[106]: 
group
1                 Int64Index([0, 1, 2], dtype='int64')
2              Int64Index([3, 4, 5, 6], dtype='int64')
3                 Int64Index([7, 8, 9], dtype='int64')
4    Int64Index([10, 11, 12, 13, 14, 15], dtype='in...
5      Int64Index([16, 17, 18, 19, 20], dtype='int64')
Name: group, dtype: object

Upvotes: 2

Related Questions