Search code examples
pythonpython-3.xjupyter-notebookdata-analysisdata-cleaning

How do I get the data in between two Column values?


I want to grab the data between column df['Instrument']'s 'Closed Qty' and 'Symbol'. Please refer to the pic. 'Closed Qty' and 'Symbol' both are values of column df['Instrument']. So, I want to grab all those values in between the row 'Closed Qty' and 'Symbol' and including the row 'Closed Qty' and 'Symbol'.

enter image description here


Solution

  • You could try following expression:

    idx = df.index[np.where((df.index >= df[df['Instrument'] == 'Closed Qty'].index[0]) & (df.index <= df[df['Instrument'] == 'Symbol'].index[0]))]
    df[df.index == idx]
    

    Here (df.index >= df[df['Instrument'] == 'Closed Qty'].index[0]) finds rows below Closed Qty (including it), the expression (df.index <= df[df['Instrument'] == 'Symbol'].index[0]) finds rows before Symbol (including it as well). Numpy where() combines both those expressions finding values between Closed Qty and Symbol.