如果我有一些虚构的数据...如何只打印在名为 的列中找到的最大值的索引(日期和时间戳)Temperature?import numpy as npimport pandas as pdnp.random.seed(11)rows,cols = 50000,2data = 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.9999949674183947Temperature 0.999995Value 0.518413Name: 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: float64和Value 0.518413列......感谢任何提示和技巧
2 回答
呼唤远方
TA贡献1856条经验 获得超11个赞
你的 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'
慕斯王
TA贡献1864条经验 获得超2个赞
最高温度可以有超过 1 个条目。
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
添加回答
举报
0/150
提交
取消