Home > Enterprise >  Retrieve Pandas dataframe rows that are consecutively equal to the values of a list
Retrieve Pandas dataframe rows that are consecutively equal to the values of a list

Time:08-07

How do I retrieve Pandas dataframe rows that are consecutively equal to the values of a list?

Given this:

import pandas as pd     

df = pd.DataFrame({'col1': [10, 20, 30, 40, 50, 88, 99, 30, 40, 50]})

lst = [30, 40, 50]

I want to extract the dataframe rows from 30 to 50, but just the first sequence of consecutive values (just the 2 to 4 index rows).

CodePudding user response:

You can use a rolling comparison:

s = df['col1'][::-1].rolling(len(lst)).apply(lambda x: x.eq(lst[::-1]).all())[::-1.eq(1)

if s.any():
    idx = s.idxmax()
    out = df.iloc[idx:idx len(lst)]
    print(out)
else:
    print('Not found')                                                                            
                                                                              

output:

   col1
2    30
3    40
4    50

CodePudding user response:

this should do the trick:

df = pd.DataFrame({'col1': [10, 20, 30, 40, 50, 88, 99, 30, 40, 50]})
lst = [30, 40, 50]
ans=[]
for i,num in enumerate(df['col1']):
    if num in lst:
        lst.remove(num)
        ans.append(i)

print(ans)
  • Related