繁体   English   中英

我想 select pandas df 中的一个值取决于另一个列值

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

我有以下 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

现在我想分配high列中值最高的行的时间戳。

我可以设法在high列中找到最高价格,但不知道如何“获取”它的时间戳?

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

由于您的时间戳是索引,请使用idxmax ,这将直接为您提供时间戳:

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

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

如果需要,然后切片最高价格:

df.loc[idx_max, 'high']

output: 126.32

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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