Home > Net >  How to fetch Index of row in pandas when certain condition has been met?
How to fetch Index of row in pandas when certain condition has been met?

Time:10-04

I am dealing with OHLC data of a stock in pandas. I have set 'date' column as index.

max_high = df['2021']['high'].max() shall find the highest value in 'high' column for the year 2021.

I intend to find the index of that row in which such high exists, so that I can find minimum value of low between a given date to date of such high by using something like:

min_low=df['2020-01-02':'2021-01-03]['low'].min()

I'm a beginner in pandas, any suggestions would be welcome. Thank you.

CodePudding user response:

Use idxmax:

df['2021']['high'].max()

Second code use idxmin:

df['2020-01-02':'2021-01-03']['low'].idxmin()

CodePudding user response:

For find index you can use idxmax and idxmin:

>>> max_high = df['2021']['high'].idxmax() 

>>> min_low = df['2020-01-02':'2021-01-03']['low'].idxmin()
  • Related