如何使用分组依据从熊猫获取MAX,MIN,SUM值

时间:2019-05-22 22:48:36

标签: python pandas

我在下面有一个数据框,我想获取MAX每小时温度,MIN每小时温度和SUM每小时降水。这是我当前的代码。我想为每个field id打印MAX值。要完成此操作,我的代码需要更改什么?当前代码不打印最大值。它输出23:00的最后一个值。我希望我的输出看起来像这样

enter image description here

import pandas
import pd as pandas

hrly_df = pd.DataFrame({'dateTime' :[t], 'field id': [id_], 'HourlyPrecipIn': [aPreVJ],'HourlyRH' : [aHumidVJ], 'HourlyTempF' : [aTempVJ]})

tempMax = hrly_df.loc[hrly_df.groupby('field id')['HourlyTempF'].idxmax()]

dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 01:00:00 40238 0.0 73.8 48.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 02:00:00 40238 0.0 77.0 46.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 03:00:00 40238 0.0 79.9 47.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 04:00:00 40238 0.0 80.6 46.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 05:00:00 40238 0.0 82.6 45.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 06:00:00 40238 0.0 85.8 45.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 07:00:00 40238 0.0 80.2 46.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 08:00:00 40238 0.0 71.3 50.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 09:00:00 40238 0.0 69.8 50.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 10:00:00 40238 0.0 62.7 53.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 11:00:00 40238 0.0 62.2 54.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 12:00:00 40238 0.0 52.4 55.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 13:00:00 40238 0.0 50.9 57.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 14:00:00 40238 0.0 46.7 58.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 15:00:00 40238 0.0 47.8 57.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 16:00:00 40238 0.01 47.1 55.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 17:00:00 40238 0.04 61.1 52.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 18:00:00 40238 0.03 80.0 48.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 19:00:00 40238 0.04 88.1 46.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 20:00:00 40238 0.08 93.8 45.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 21:00:00 40238 0.07 93.0 45.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 22:00:00 40238 0.08 93.0 45.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 23:00:00 40238 0.09 92.7 45.1 Starting import of field id: 3402 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 01:00:00 3402 0.0 73.9 48.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 02:00:00 3402 0.0 77.1 46.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 03:00:00 3402 0.0 79.9 47.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 04:00:00 3402 0.0 80.6 46.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 05:00:00 3402 0.0 82.6 45.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 06:00:00 3402 0.0 85.6 45.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 07:00:00 3402 0.0 80.2 47.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 08:00:00 3402 0.0 71.3 50.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 09:00:00 3402 0.0 69.7 50.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 10:00:00 3402 0.0 62.8 53.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 11:00:00 3402 0.0 62.2 54.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 12:00:00 3402 0.0 52.6 55.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 13:00:00 3402 0.0 50.9 57.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 14:00:00 3402 0.0 46.8 58.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 15:00:00 3402 0.0 47.8 57.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 16:00:00 3402 0.01 47.2 55.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 17:00:00 3402 0.03 61.1 52.5 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 18:00:00 3402 0.03 79.9 48.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 19:00:00 3402 0.04 88.2 46.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 20:00:00 3402 0.08 93.7 45.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 21:00:00 3402 0.07 93.0 45.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 22:00:00 3402 0.07 93.0 45.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 23:00:00 3402 0.08 92.7 45.1 Starting import of field id: 45883 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 01:00:00 45883 0.0 75.4 48.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 02:00:00 45883 0.0 77.6 47.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 03:00:00 45883 0.0 79.6 47.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 04:00:00 45883 0.0 80.2 47.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 05:00:00 45883 0.0 82.5 45.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 06:00:00 45883 0.0 84.7 45.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 07:00:00 45883 0.0 79.2 47.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 08:00:00 45883 0.0 71.9 50.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 09:00:00 45883 0.0 68.9 51.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 10:00:00 45883 0.0 63.0 53.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 11:00:00 45883 0.0 61.8 54.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 12:00:00 45883 0.0 52.9 56.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 13:00:00 45883 0.0 50.9 57.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 14:00:00 45883 0.0 48.4 58.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 15:00:00 45883 0.0 48.3 57.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 16:00:00 45883 0.02 48.2 55.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 17:00:00 45883 0.07 63.0 52.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 18:00:00 45883 0.02 79.8 48.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 19:00:00 45883 0.05 89.4 47.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 20:00:00 45883 0.08 93.4 45.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 21:00:00 45883 0.07 93.2 45.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 22:00:00 45883 0.04 92.8 45.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-21 23:00:00 45883 0.1 92.3 45.6

2 个答案:

答案 0 :(得分:0)

我不确定您要的是什么,所以我会尽力而为,直到您可以提供验证:

.latest

答案 1 :(得分:0)

您可以使用带有功能列表的agg

hrly_df.groupby('field id')['HourlyTempF'].agg(['min','max','sum'])
Out[313]: 
           min   max    sum
field id                   
3402      46.8  48.4   95.2
40238     46.8  48.4  142.2