如何在整个熊猫而不是每一行中使用整个数据框的groupby获取最大值

时间:2019-05-23 16:53:40

标签: python pandas dataframe

我的数据框输出如下所示。我想对field id进行分组,以获取24的max中的HourlyTempF minHourlyTempF sumHourlyPrecipIn小时时段。我当前的解决方案给出了每一行的最大值,顺便说一句又是整个数据帧。我应该为每个字段ID设置三个值。

我认为问题在于数据框中的每一行都是“自己的数据框”,每一行都有标题和列,因此当我得到maxminsum时,获取每小时的每一行的当前值,我实际上只是想对整个数据框进行分组,以便为​​我提供如下所示的输出,并根据所需输出中的值创建变量,以便将它们放在另一个数据框中。

我的代码:

`import pandas as pd
 import pandas


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


hrly_df = hrly_df[['dateTime','field id','HourlyPrecipIn','HourlyRH', 'HourlyTempF']]
hrly_df.head()
hrlydfs = hrLylst.append(hrly_df)


#GETS EACH MAX ROW INSTEAD OF MAX FOR DF
tempMax= hrly_df.groupby('field id')['HourlyTempF'].agg(['max'])
tempMax2 = tempMax.max().max()

# print 'Data successfully collected - writing to csv...'

tempDf = pd.DataFrame({'date' :[config.dayVal ], 'field id': [id_], 'DailyHighF': ['SHOULD BE MAX FROM hrly_df'],'DailyLowF' : ['SHOULD BE MIN FROM HRLY DF'], 'DailyPrecipIn' : ['SHOULD BE TOTAL FROM HRLY DF']})

我打印tempMax时的当前输出

Starting import of field id: 40238 44.9 45.1 45.1 45.3 46.7 46.7 48.6 50.2 52.1 54.0 54.3 54.5 54.7 54.5 56.4 56.6 55.7 54.0 54.0 54.1 54.1 53.6 52.2 Starting import of field id: 3402 44.9 45.1 45.1 45.3 46.7 46.7 48.6 50.2 52.1 54.0 54.4 54.5 54.7 54.5 56.5 56.6 55.7 54.1 54.0 54.1 54.2 53.6 52.2 Starting import of field id: 45883 45.3 45.6 45.7 45.9 47.1 47.3 49.1 50.7 52.7 54.3 54.8 55.0 55.2 55.0 57.1 57.5 56.2 54.6 54.4 54.6 54.6 53.8 52.7

所需的输出:

field id | max temp  | min temp | total precip
40238      56.4         44.9        0.06
3402       56.6         44.9        0.06
45883      57.7         45.3        0.06

当前数据框 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 01:00:00 40238 0.0 98.8 44.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 02:00:00 40238 0.0 98.9 45.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 03:00:00 40238 0.0 98.7 45.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 04:00:00 40238 0.02 99.6 45.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 05:00:00 40238 0.0 95.0 46.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 06:00:00 40238 0.0 99.8 46.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 07:00:00 40238 0.02 95.6 48.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 08:00:00 40238 0.0 94.4 50.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 09:00:00 40238 0.01 93.6 52.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 10:00:00 40238 0.0 93.6 54.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 11:00:00 40238 0.01 93.5 54.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 12:00:00 40238 0.0 87.3 54.5 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 13:00:00 40238 0.0 86.1 54.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 14:00:00 40238 0.0 88.0 54.5 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 15:00:00 40238 0.0 82.1 56.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 16:00:00 40238 0.0 85.5 56.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 17:00:00 40238 0.0 82.9 55.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 18:00:00 40238 0.0 82.6 54.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 19:00:00 40238 0.0 79.1 54.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 20:00:00 40238 0.0 83.8 54.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 21:00:00 40238 0.0 87.9 54.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 22:00:00 40238 0.0 88.6 53.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 23:00:00 40238 0.0 87.5 52.2 Starting import of field id: 3402 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 01:00:00 3402 0.0 98.7 44.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 02:00:00 3402 0.0 98.8 45.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 03:00:00 3402 0.0 98.7 45.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 04:00:00 3402 0.01 99.5 45.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 05:00:00 3402 0.0 95.0 46.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 06:00:00 3402 0.0 99.7 46.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 07:00:00 3402 0.02 95.6 48.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 08:00:00 3402 0.0 94.5 50.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 09:00:00 3402 0.01 93.6 52.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 10:00:00 3402 0.0 93.6 54.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 11:00:00 3402 0.01 93.5 54.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 12:00:00 3402 0.0 87.3 54.5 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 13:00:00 3402 0.0 86.0 54.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 14:00:00 3402 0.0 87.9 54.5 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 15:00:00 3402 0.0 82.0 56.5 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 16:00:00 3402 0.0 85.4 56.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 17:00:00 3402 0.0 82.9 55.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 18:00:00 3402 0.0 82.6 54.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 19:00:00 3402 0.0 79.2 54.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 20:00:00 3402 0.0 83.8 54.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 21:00:00 3402 0.0 87.9 54.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 22:00:00 3402 0.0 88.6 53.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 23:00:00 3402 0.0 87.5 52.2 Starting import of field id: 45883 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 01:00:00 45883 0.0 97.9 45.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 02:00:00 45883 0.0 97.9 45.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 03:00:00 45883 0.0 97.7 45.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 04:00:00 45883 0.0 99.0 45.9 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 05:00:00 45883 0.0 95.5 47.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 06:00:00 45883 0.0 99.0 47.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 07:00:00 45883 0.03 95.3 49.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 08:00:00 45883 0.0 95.2 50.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 09:00:00 45883 0.01 94.0 52.7 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 10:00:00 45883 0.02 93.3 54.3 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 11:00:00 45883 0.04 92.9 54.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 12:00:00 45883 0.0 86.9 55.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 13:00:00 45883 0.0 84.7 55.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 14:00:00 45883 0.0 87.3 55.0 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 15:00:00 45883 0.0 81.9 57.1 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 16:00:00 45883 0.0 83.4 57.5 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 17:00:00 45883 0.0 82.5 56.2 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 18:00:00 45883 0.0 82.1 54.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 19:00:00 45883 0.0 80.1 54.4 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 20:00:00 45883 0.0 83.9 54.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 21:00:00 45883 0.0 87.4 54.6 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 22:00:00 45883 0.0 88.4 53.8 dateTime field id HourlyPrecipIn HourlyRH HourlyTempF 0 2019-05-22 23:00:00 45883 0.0 87.5 52.7

1 个答案:

答案 0 :(得分:1)

请注意,由于复制/粘贴数据中引入的错误,我看到的值可能会略有不同,但是最简单的获取方法(我假设)是您的输出是在列上使用SELECT country FROM `user` WHERE created > now() - interval 9 day GROUP BY country ORDER BY count(*) desc LIMIT 1 groupby函数和聚合字典。

这是我的测试DF中数据的外观:

agg()

如果您的数据是作为单独的行进入的,那么将其放在单个数据框中可能完全是另一个问题。

    dateTime    field       id      HourlyPrecipIn  HourlyRH    HourlyTempF
0   2019-05-22  01:00:00    40238   0.0             98.8        44.9
0   2019-05-22  02:00:00    40238   0.0             98.9        45.1
0   2019-05-22  03:00:00    40238   0.0             98.7        45.1
0   2019-05-22  04:00:00    40238   0.02            99.6        45.3
0   2019-05-22  05:00:00    40238   0.0             95.0        46.7
0   2019-05-22  06:00:00    40238   0.0             99.8        46.7