Marco
Marco

Reputation: 231

Retrieve Pandas dataframe rows that its column (one) values are consecutively equal to the values of a list

How do I retrieve Pandas dataframe rows that its column (one) values are consecutively equal to the values of a list?

Example, 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).

Upvotes: 2

Views: 98

Answers (3)

mozway
mozway

Reputation: 260745

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

Upvotes: 2

Andrej Kesely
Andrej Kesely

Reputation: 195438

Try:

lst = [30, 40, 50]

if any(lst == (found := s).to_list() for s in df["col1"].rolling(len(lst))):
    print(df.loc[found.index])

Prints:

   col1
2    30
3    40
4    50

Upvotes: 0

Ohad Sharet
Ohad Sharet

Reputation: 1142

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)

Upvotes: 2

Related Questions