how to get rows satisfying certain condition pandas

aniket patil
 name       strike

INFY        1000

INFY        1020

INFY        1040

INFY        1060

INFY        1080

INFY        1100

INFY        1120

INFY        1140

INFY        1160

INFY        1180

INFY        1200

INFY        1220

I have a dataframe containing columns name and strike,

for query ltp = 1065 I want to return dataframe containing 6 rows

where three rows will have value greater than ltp and three rows will have value lower than ltp

in this case

INFY        1020

INFY        1040

INFY        1060

INFY        1080

INFY        1100

INFY        1120

.

how can i achieve this?

Corralien

Input data:

>>> df
    name  strike  # ltp > strike
0   INFY    1000  # False
1   INFY    1020  # False                                 <-|
2   INFY    1040  # False                                 <-|
3   INFY    1060  # False                                 <-|
4   INFY    1080  # True <- this is the breakpoint, idx=4 <-|
5   INFY    1100  # True                                  <-|
6   INFY    1120  # True                                  <-|
7   INFY    1140  # True
8   INFY    1160  # True
9   INFY    1180  # True
10  INFY    1200  # True
11  INFY    1220  # True

Find the breakpoint with argmin and get 3 items below and 3 items above including the breakpoint:

>>> idx = df['strike'].lt(ltp).argmin()
>>> out = df['strike'].iloc[max(idx-3, 0):min(idx+3, len(df))]

Output result:

>>> out
1    1020
2    1040
3    1060
4    1080
5    1100
6    1120
Name: strike, dtype: int64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas DataFrame: Remove row satisfying certain condition

Pandas: sum consecutive rows satisfying condition

pandas df filter on rows with any n rows satisfying condition

How can I get the names of rows in a data.table satisfying a regex condition?

An efficient way to find rows in pandas with columns satisfying certain conditions

Using Pandas to drop the extra rows after satisfying a condition

Julia: how to select all columns from a DataFrame satisfying a certain condition?

How do i extract certain values in a row satisfying a condition in a dataframe?

SQL how to keep multiple entries satisfying a certain condition

Pandas DataFrame get rows where index matches a certain condition

Get percentage of rows (strings) that fulfil a certain condition in a pandas data frame

Delete rows with a certain condition in pandas

How to easily create variable with number of group rows satisfying treatment condition?

Get pairs of values satisfying condition based on mutual connection pandas

How to get index number of rows with condition in pandas

Using SQL, how to get rows satisfying one of multiple conditions with additional boolean columns displaying which exact condition was satisfied

Pandas: How to delete rows that do not match certain condition with a counter?

How to delete certain rows in pandas which satisfies some condition

How to remove duplicate rows in dataframe In pandas depending upon certain condition

SQL: How to get all rows that it's columns meet a certain condition

how to extract certain rows with a condition?

Remove elements by value satisfying certain condition

Is there a way to count the number of columns satisfying a certain condition?

Drop rows WHERE date is a certain condition Pandas

How to get the first item in a group by that meets a certain condition in pandas?

How to get the cartesian product of a pandas dataframe under certain condition

Delete rows satisfying condition for each column in R

Using Pandas, how to I apply a formula to several rows, based on the average of previous rows given a certain condition?

How to get rows that has certain columns containing same values in pandas?