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?
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.
Comments