使用Pandas和Python3在特定时间获取列的值

时间:2018-04-09 12:53:43

标签: python-3.x pandas pandas-groupby

我一直在使用熊猫几周,所以我还在探索它。

我创建了一个每小时43分钟更新一次的DataFrame。 DF的内容是玩家统计数据,我按年份对内容进行了分组,因为我想知道每一天特定栏目中捐赠的最大值是多少'。捐款的价值'周日午夜重置,因此整个星期都会继续增长。我希望在同一个DF中有另一列,最大值为每天17.44。 现在我想出了:

originalDF['date']=originalDF['date'].apply(lambda x:
                                        dt.datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
originalDF['DayofYear']=originalDF['date'].dt.dayofyear
highestDonationByTheDay=originalDF.groupby(['DayofYear', 'name']).max()

这个问题是我无法控制一天中的时间,它只返回当天最大值23.43。

有关如何实现这一目标的任何帮助?

这是originalDF的摘录:

date              donations  A    B     name                C        D
2018-04-08 13:43:11    912    192  720    Eisenheim       80QQVCQ8G   4240
2018-04-08 13:43:11    785    25   760    El Mariachi     RUJL8R9Y    4108
2018-04-08 13:43:11    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 13:43:11    915    155  760    Skoy            8RLVV20PY   4065
2018-04-08 13:43:11    1373   613  760    TheSonicSliver  9LG8QV2G    4207
2018-04-08 14:43:14    922    202  720    Eisenheim       80QQVCQ8G   4240
2018-04-08 14:43:14    785    25   760    El Mariachi     RUJL8R9Y    4078
2018-04-08 14:43:14    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 14:43:14    915    155  760    Skoy            8RLVV20PY   4065
2018-04-08 14:43:14    1373   613  760    TheSonicSliver  9LG8QV2G    4207
2018-04-08 15:43:15    922    202  720    Eisenheim       80QQVCQ8G   4240
2018-04-08 15:43:15    803    43   760    El Mariachi     RUJL8R9Y    4108
2018-04-08 15:43:15    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 15:43:15    915    155  760    Skoy            8RLVV20PY   4065
2018-04-08 15:43:15    1381   581  800    TheSonicSliver  9LG8QV2G    4207
2018-04-08 16:43:13    964    244  720    Eisenheim       80QQVCQ8G   4240
2018-04-08 16:43:13    827    47   780    El Mariachi     RUJL8R9Y    4108
2018-04-08 16:43:13    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 16:43:13    933    173  760    Skoy            8RLVV20PY   4065
2018-04-08 16:43:13    1389   589  800    TheSonicSliver  9LG8QV2G    4207
2018-04-08 17:43:17    964    204  760    Eisenheim       80QQVCQ8G   4240
2018-04-08 17:43:17    827    27   800    El Mariachi     RUJL8R9Y    4051
2018-04-08 17:43:17    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 17:43:17    958    198  760    Skoy            8RLVV20PY   4065
2018-04-08 17:43:17    1439   639  800    TheSonicSliver  9LG8QV2G    4207
2018-04-08 18:43:17    984    224  760    Eisenheim       80QQVCQ8G   4240
2018-04-08 18:43:17    827    27   800    El Mariachi     RUJL8R9Y    4021
2018-04-08 18:43:17    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 18:43:17    958    198  760    Skoy            8RLVV20PY   4065
2018-04-08 18:43:17    1439   639  800    TheSonicSliver  9LG8QV2G    4207
2018-04-08 19:43:14    984    224  760    Eisenheim       80QQVCQ8G   4240
2018-04-08 19:43:14    827    27   800    El Mariachi     RUJL8R9Y    4021
2018-04-08 19:43:14    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 19:43:14    958    198  760    Skoy            8RLVV20PY   4065
2018-04-08 19:43:14    1441   641  800    TheSonicSliver  9LG8QV2G    4207
2018-04-08 20:43:11    984    224  760    Eisenheim       80QQVCQ8G   4240
2018-04-08 20:43:11    827    27   800    El Mariachi     RUJL8R9Y    4021
2018-04-08 20:43:11    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 20:43:11    958    158  800    Skoy            8RLVV20PY   4065
2018-04-08 20:43:11    1441   641  800    TheSonicSliver  9LG8QV2G    4207
2018-04-08 21:43:13    984    224  760    Eisenheim       80QQVCQ8G   4240
2018-04-08 21:43:13    827    27   800    El Mariachi     RUJL8R9Y    4021
2018-04-08 21:43:13    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 21:43:13    958    158  800    Skoy            8RLVV20PY   4065
2018-04-08 21:43:13    1445   645  800    TheSonicSliver  9LG8QV2G    4207
2018-04-08 22:43:13    994    234  760    Eisenheim       80QQVCQ8G   4240
2018-04-08 22:43:13    827    27   800    El Mariachi     RUJL8R9Y    4021
2018-04-08 22:43:13    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 22:43:13    958    158  800    Skoy            8RLVV20PY   4065
2018-04-08 22:43:13    1445   645  800    TheSonicSliver  9LG8QV2G    4207
2018-04-08 23:43:12    1032   272  760    Eisenheim       80QQVCQ8G   4240
2018-04-08 23:43:12    827    27   800    El Mariachi     RUJL8R9Y    4021
2018-04-08 23:43:12    690    210  480    macomaco        UJP8LVUL    3980
2018-04-08 23:43:12    958    158  800    Skoy            8RLVV20PY   4065
2018-04-08 23:43:12    1473   633  840    TheSonicSliver  9LG8QV2G    4207

1 个答案:

答案 0 :(得分:2)

如果你想在17.44之前获得最长时间,你可以这样做:

maxDF = originalDF.set_index('date').between_time('00:00:00', '17:44:00').reset_index()
maxDF['DayofYear'] = maxDF['date'].dt.dayofyear
highestDonationByTheDay = maxDF.groupby(['DayofYear', 'name']).max()

大量借鉴:

Conditional selection before certain time of day - Pandas dataframe

Select rows per groups and with time conditions