猿问

熊猫数据框中的最大值和最小值

我有一个 pandas 数据框,它显示 1990 年的每小时温度读数,如下所示:


           Date and time  Dry bulb temperature

0    1990-01-01 00:00:00                   8.2

1    1990-01-01 01:00:00                   8.1

2    1990-01-01 02:00:00                   8.3

3    1990-01-01 03:00:00                   8.5

4    1990-01-01 04:00:00                   8.8

...                  ...                   ...

8755 1990-12-31 19:00:00                   3.0

8756 1990-12-31 20:00:00                   2.6

8757 1990-12-31 21:00:00                   2.8

8758 1990-12-31 22:00:00                   4.2

8759 1990-12-31 23:00:00                   2.0

我想每 24 小时计算一次最大干球温度并获得相应的日期和时间。我该怎么办?


到目前为止,我有:


o=[]

for i in range(0, len(Dataframe['Dry bulb temperature']), 24):

    ymax = np.max(Dataframe['Dry bulb temperature'][i:i+24])

    o.append(ymax)

print(o)

它每 24 小时给出一次最高温度,如下所示:


[9.7, 9.9, 8.4, 10.4, 11.2, 12.0, 10.5, 10.7, 11.9, 12.0, 11.5, 11.4, 10.2, 10.9, 13.6, 11.5, 9.6, 10.9, 10.8, 12.3, 12.3, 12.2, 11.5, 7.9, 12.7, 6.0, 9.4, 8.2, 9.8, 10.6, 9.6, 8.8, 10.8, 8.6, 11.9, 11.7, 12.2, 13.8, 12.5, 10.8, 13.2, 8.2, 7.4, 12.1, 12.4, 8.6, 7.7, 12.3, 13.3, 12.3, 13.1, 12.0, 12.7, 11.5, 12.7, 12.5, 12.5, 8.7, 13.2, 7.7, 9.0, 10.1, 10.6, 10.9, 11.9, 11.4, 13.3, 12.2, 15.0, 14.1, 13.1, 12.9, 13.7, 12.7, 12.7, 16.3, 14.9, 12.8, 11.8, 14.2, 11.5, 11.7, 10.4, 10.1, 9.9, 9.6, 10.6, 12.7, 16.0, 15.3, 14.4, 14.2, 8.6, 7.0, 9.8, 11.6, 12.6, 11.1, 12.3, 12.2, 14.8, 15.2, 11.3, 12.1, 12.0, 12.3, 11.5, 10.8, 10.0, 11.7, 15.3, 12.9, 17.0, 17.6, 18.9, 14.2, 13.3, 14.9, 17.8, 20.6, 21.9, 24.1, 26.8, 25.4, 24.9, 23.5, 16.4, 14.9, 13.8, 14.2, 17.7, 17.9, 16.8, 15.7, 16.3, 18.9, 19.4, 18.3, 14.5, 17.6, 18.8, 18.1, 21.9, 18.2, 14.7, 14.9, 19.4, 20.0, 14.9, 18.9, 16.8, 17.6, 15.8, 14.6, 17.0, 

我想以表格形式获取每个最高温度的相应日期:


[9.7,1990-01-02 03:00:00],...,etc. 


慕尼黑的夜晚无繁华
浏览 77回答 2
2回答

肥皂起泡泡

你可以使用这个:df['Date and time'] = pd.to_datetime(df['Date and time'])df1 = df.set_index('Date and time').resample('D')['Dry bulb temperature'].agg({'max':'max', 'min':'min'})它为您的问题中的可见数据提供了以下输出:               max  minDate and time          1990-01-01     8.8  8.11990-12-31     4.2  2.0如果您真的希望将结果作为列表,您可以在之后使用它:df1.reset_index().to_numpy()[array([Timestamp('1990-01-01 00:00:00'), 8.8, 8.1], dtype=object), array([Timestamp('1990-12-31 00:00:00'), 4.2, 2.0], dtype=object)]要获得每天最大值的确切日期时间,您可以尝试以下操作:df2 = df.set_index('Date and time')df2.loc[df2.groupby(df2.index.dayofyear).idxmax().iloc[:, 0]]                     Dry_bulb_temperatureDate_and_time                            1990-01-01 04:00:00                   8.81990-12-31 22:00:00                   4.2

守着星空守着你

你可以尝试使用这个:from datetime import timedeltaday = min(df['Date and time'])max_day = max(df['Date and time'])results = list()while day <= max_day:&nbsp; &nbsp; # small part of dataframe&nbsp; &nbsp; temp = df[(df['Date and time'] >= day) & (df['Date and time'] < day + timedelta(1))]&nbsp; &nbsp; # Row with max temprature&nbsp; &nbsp; row = df.iloc[temp['Dry bulb temperature'].idxmax()]&nbsp; &nbsp; results.append([row['Dry bulb temperature'], row['Date and time']])&nbsp; &nbsp; day += timedelta(1)
随时随地看视频慕课网APP

相关分类

Python
我要回答