Generating A Tuple Of Indexes Based On A Sequence Of Values In A Pandas Dataframe
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
Solution 1:
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)]
Post a Comment for "Generating A Tuple Of Indexes Based On A Sequence Of Values In A Pandas Dataframe"