Home > Back-end >  I want to select a value in a pandas df depending on another columns value
I want to select a value in a pandas df depending on another columns value

Time:01-02

I have the following df prices:

                               open      high       low     close   
timestamp                                                                                           
2021-06-08 07:30:00 00:00  126.1000  126.1600  125.9600  126.0600
2021-06-08 08:15:00 00:00  126.0500  126.3000  126.0500  126.3000
2021-06-08 09:00:00 00:00  126.2700  126.3200  126.2200  126.2800

Now I want to assign the timestamp of the row whose value in column high is the highest.

I could manage to find the highest price in the high column but don't know how to "grab" its timestamp?

high_prices = prices['high']
highest_price = high_prices.max()
timestamp_highest_price = ?

CodePudding user response:

As you timestamps are the index, use idxmax, this will give your directly the timestamp:

idx_max = df['high'].idxmax()

output: '2021-06-08 09:00:00 00:00'

Then slice the max price if needed:

df.loc[idx_max, 'high']

output: 126.32

  • Related