【问题标题】:How to get the max value for every date in pandas dataframe如何获取熊猫数据框中每个日期的最大值
【发布时间】:2021-08-09 15:42:50
【问题描述】:

我有一个数据框,其中包含从 2016 年 1 月 1 日到 2020 年 12 月 31 日的时间序列格式的 UV 值。

print(df.head(24))

         date      time  uv_index
0  2016-01-01  00:00:00       1.0
1  2016-01-01  01:00:00       1.0
2  2016-01-01  02:00:00       1.0
3  2016-01-01  03:00:00       1.0
4  2016-01-01  04:00:00       1.0
5  2016-01-01  05:00:00       1.0
6  2016-01-01  06:00:00       1.0
7  2016-01-01  07:00:00       1.0
8  2016-01-01  08:00:00       1.0
9  2016-01-01  09:00:00       1.0
10 2016-01-01  10:00:00       1.0
11 2016-01-01  11:00:00       1.0
12 2016-01-01  12:00:00       1.0
13 2016-01-01  13:00:00       1.0
14 2016-01-01  14:00:00       1.0
15 2016-01-01  15:00:00       1.0
16 2016-01-01  16:00:00       1.0
17 2016-01-01  17:00:00       2.0
18 2016-01-01  18:00:00       1.0
19 2016-01-01  19:00:00       1.0
20 2016-01-01  20:00:00       1.0
21 2016-01-01  21:00:00       1.0
22 2016-01-01  22:00:00       1.0
23 2016-01-01  23:00:00       1.0

如何创建一个新列 uv_max,它采用每个日期的最大 uv_index 值。

print(df1.head(24))

         date      time  uv_index  uv_max
0  2016-01-01  00:00:00       1.0     2.0
1  2016-01-01  01:00:00       1.0     2.0
2  2016-01-01  02:00:00       1.0     2.0
3  2016-01-01  03:00:00       1.0     2.0
4  2016-01-01  04:00:00       1.0     2.0
5  2016-01-01  05:00:00       1.0     2.0
6  2016-01-01  06:00:00       1.0     2.0
7  2016-01-01  07:00:00       1.0     2.0
8  2016-01-01  08:00:00       1.0     2.0
9  2016-01-01  09:00:00       1.0     2.0
10 2016-01-01  10:00:00       1.0     2.0
11 2016-01-01  11:00:00       1.0     2.0
12 2016-01-01  12:00:00       1.0     2.0
13 2016-01-01  13:00:00       1.0     2.0
14 2016-01-01  14:00:00       1.0     2.0
15 2016-01-01  15:00:00       1.0     2.0
16 2016-01-01  16:00:00       1.0     2.0
17 2016-01-01  17:00:00       2.0     2.0
18 2016-01-01  18:00:00       1.0     2.0
19 2016-01-01  19:00:00       1.0     2.0
20 2016-01-01  20:00:00       1.0     2.0
21 2016-01-01  21:00:00       1.0     2.0
22 2016-01-01  22:00:00       1.0     2.0
23 2016-01-01  23:00:00       1.0     2.0

【问题讨论】:

    标签: python pandas date datetime


    【解决方案1】:

    使用groupby()+transform():

    df['uv_max']=df.groupby('date')['uv_index'].transform('max')
    

    【讨论】:

      猜你喜欢
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 2021-09-25
      • 2019-01-17
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多