简体   繁体   中英

I want to select a value in a pandas df depending on another columns value

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 = ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM