Home > OS >  Pandas DataFrame, subset previous 3 rows to rows that meets condition?
Pandas DataFrame, subset previous 3 rows to rows that meets condition?

Time:10-22

I want to subset previous 3 rows up to the row where the given condition is met. Is there a way to do it rather than get the index values and subset them step by step?

Also, I want to see the previous 3 rows to the row that meets the condition plus 3 rows after it.

index col2
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 i
9 j
  1. Say, row 4 meets the condition, and I want to see row 2, row 3 and row 4.

  2. I want to show from row 2 to 7 subset by the row 4 that meets a condition.

CodePudding user response:

Comapre value of column, shifting for Series.shift get starting row (here 3 previous including actual row, so subtract 1), repeating Trues by Series.cummax, filtering in boolean indexing and last select 2 * N rows in DataFrame.head:

val = 'e'
N = 3

df = df[df.col2.shift(-N   1).eq(val).cummax()].head(N * 2)
print (df)
  col2
2    c
3    d
4    e
5    f
6    g
7    h

First solution:

val = 'e'
N = 3

df = df[~df.col2.shift().eq(val).cummax()].tail(N)
print (df)
  col2
2    c
3    d
4    e
  • Related