如何将日期分组到大熊猫中

问题描述 投票:0回答:1
    Datos
2015-01-01  58
2015-01-02  42
2015-01-03  41
2015-01-04  13
2015-01-05  6
...     ...
2020-06-18  49
2020-06-19  41
2020-06-20  23
2020-06-21  39
2020-06-22  22

2000 rows × 1 columns

我有一个数据集,它由一列数据组成,其数据代表了某年期间每天的平均温度。我想知道如何得到每天的最大值(考虑到一年有365天),并得到一个类似于此的df。

        Datos
1   40
2   50
3   46
4   8
5   26
...     ...
361     39
362     23
363     23
364     37
365     25

365 rows × 1 columns

请原谅我的无知,非常感谢您的帮助。

python python-3.x pandas pandas-groupby python-datetime
1个回答
1
投票

你可以这样做。

df['Date'] = pd.to_datetime(df['Date'])
df = df.groupby(by=pd.Grouper(key='Date', freq='D')).max().reset_index()
df['Day'] = df['Date'].dt.dayofyear
print(df)

           Date  Temp  Day
0    2015-01-01  58.0    1
1    2015-01-02  42.0    2
2    2015-01-03  41.0    3
3    2015-01-04  13.0    4
4    2015-01-05   6.0    5
...         ...   ...  ...
1995 2020-06-18  49.0  170
1996 2020-06-19  41.0  171
1997 2020-06-20  23.0  172
1998 2020-06-21  39.0  173
1999 2020-06-22  22.0  174

0
投票

新建一列

df["day of year"] = df.Datos.dayofyear

然后...

df.groupby("day of year").max()
© www.soinside.com 2019 - 2024. All rights reserved.