pandas 打印在带有 f 字符串的列中找到的最大值
pandas printing maximum value found in a column with an f string
如果我有一些虚构的数据...如何只打印在名为 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
最终我希望创建一个只打印 maximum temperature recorded in the dataset is 0.999995 found on 2023-01-06 02:00:00
的 f
字符串,而不需要额外的信息,例如 Name:
和 , dtype: float64
以及 Value 0.518413
专栏...感谢任何提示和技巧
你的 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'
最高温度的条目可以超过 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
如果我有一些虚构的数据...如何只打印在名为 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
最终我希望创建一个只打印 maximum temperature recorded in the dataset is 0.999995 found on 2023-01-06 02:00:00
的 f
字符串,而不需要额外的信息,例如 Name:
和 , dtype: float64
以及 Value 0.518413
专栏...感谢任何提示和技巧
你的 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'
最高温度的条目可以超过 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