Generating a tuple of indexes based on a sequence of values in a pandas DataFrame

Viewed 157

It's a follow up to my previous question here: Finding the index of rows based on a sequence of values in a column of pandas DataFrame

I want to get a list of tuples that has index of very bad, followed with the the index of first occurrence of 'bad':

import random

df = pd.DataFrame({
    'measure': [random.randint(0,10) for _ in range(0,20)],
})

df['status'] = df.apply(
    lambda x: 'good' if x['measure'] > 4 else 'very bad' if x['measure'] < 2  else 'bad',
    axis=1)

Here's the data frame:

    measure    status
0         8      good
1         8      good
2         0  very bad
3         5      good
4         2       bad
5         3       bad
6         9      good
7         9      good
8        10      good
9         5      good
10        1  very bad
11        7      good
12        7      good
13        6      good
14        5      good
15       10      good
16        3       bad
17        0  very bad
18        3       bad

How can I get a tuple of such combinations?

[(2,4), (10,16), (17,18)]

1 Answers

IIUC, you can try:

# filters only rows with bad and very bad
m = df[df['status'].isin(['bad','very bad'])] 

# check id current row is very bad and next row is bad
c = m['status'].eq('very bad') & m['status'].shift(-1).eq('bad')

# if true return next row as true too and get only index values
idx = m[c|c.shift()].index

# convert every 2 items into a tuple
res = [*zip(idx[::2],idx[1::2])]


[(2, 4), (10, 16), (17, 18)]
Related