Higher and Lower rows from a defined row

Viewed 47

data requiredI have following script... First I want to define the row having 2480 value in "strike" column and then need 6 above and 6 below rows data from row belonging to 2480 value in "strike" column. Means I need total 13 rows data(values). I referred some similar questions on this website but couldn't do the same except getting errors. I tried.... idx = df.index.get_loc(2480) df.iloc[idx - 6 : idx + 6]

But getting key error 2480. How to do that plz ??

To be more clear here is my final data table .... https://docs.google.com/spreadsheets/d/1TD83y9TgXy8_rZkfYkjoU4GR_1gwkt7zrF_ynC_RdsE/edit?usp=drivesdk

    from nsepython import *
    import math
    
    symbol = "ACC"
    print("ATM STRIKE = ")
    x = nse_fno(symbol)['underlyingValue']
    def round_to_multiple(number, multiple):
        return multiple * round(number / multiple)
    ATM_STRIKE = round_to_multiple(x, 20)
    print(ATM_STRIKE)
    
    raw = option_chain(symbol)
    data = raw["filtered"]["data"]
    oc_data = []
    for i in data:
        for j,k in i.items():
            if j == "CE":
                info = k
                info["instrumentType"] = j
                oc_data.append(info)
    df1 = pd.DataFrame(oc_data)
    #print(df1)
    df = df1[['openInterest','changeinOpenInterest','totalTradedVolume','lastPrice','change','impliedVolatility','strikePrice','instrumentType']]
    
    df.columns = [['OI', 'CH OI','Vol','LTP','CH','IV','STRIKES','Type']]
    pd.set_option('display.max_columns', None)
    pd.set_option('display.max_rows', None)
    #print(df1.columns)
    print(df.to_string(index=False))
0 Answers
Related