pandas打印在f字符串的列中找到的最大值。

问题描述 投票:0回答:1

如果我有一些虚构的数据... ... 我如何只打印在一列中发现的最大值的索引(日期& 时间戳),该列名为 Temperature?

import numpy as np
import pandas as pd
np.random.seed(11)

rows,cols = 50000,2
data = np.random.rand(rows,cols) 
tidx = pd.date_range('2019-01-01', periods=rows, freq='H') 
df = pd.DataFrame(data, columns=['Temperature','Value'], index=tidx)

maxy = df.Temperature.max()
maxDate = df.loc[df['Temperature'].idxmax()]

如果我打印 maxDate 有很多不需要的信息。

print(maxy)
print(maxDate)

这个输出。

0.9999949674183947
Temperature    0.999995
Value          0.518413
Name: 2023-01-06 02:00:00, dtype: float64

最终我希望创建一个 f 字符串 maximum temperature recorded in the dataset is 0.999995 found on 2023-01-06 02:00:00如果没有额外的信息,如 Name:, dtype: float64Value 0.518413 栏目... 谢谢你的任何提示&技巧

python pandas string-formatting
1个回答
0
投票

你的f字符串可以是。

f'maximum temperature recoded in the dataset is {maxy} found on {maxDate.name}'

或者没有 maxy:

f'maximum temperature recoded in the dataset is {maxDate.Temperature} found on {maxDate.name}'

产量。

'maximum temperature recoded in the dataset is 0.9999949674183947 found on 2023-01-06 02:00:00'

0
投票

最高温度可以有一个以上的条目。

maxy = df.Temperature.max()
max_dates = df[df['Temperature'] == maxy].index.astype(str).to_list()
print(f'Max temperature recorded in dataset is {maxy} on {",".join(max_dates)}')

输出:

Max temperature recorded in dataset is 0.9999949674183947 on 2023-01-06 02:00:00
© www.soinside.com 2019 - 2024. All rights reserved.