2

给定一个这样的系列

    Date
2005-01-01    128
2005-01-02     72
2005-01-03     67
2005-01-04     61
2005-01-05     33
Name: Data_Value, dtype: int64

几年来,我如何将所有 1 月 1 日、所有 1 月 2 日等组合在一起?

我实际上是在尝试找到几年中一年中每一天的最大值,所以它不必是 groupby。如果有更简单的方法可以做到这一点,那就太好了。

4

3 回答 3

3

您可以将索引转换为日期时间,然后用于strftime获取日期格式的字符串以进行分组:

df.groupby(pd.to_datetime(df.index).strftime('%b-%d'))['Date_Value'].max()

如果您的日期字符串中没有 NaN,您也可以切片。这将返回格式为“MM-DD”的字符串:

df.groupby(df.index.astype(str).str[5:])['Date_Value'].max()
于 2019-06-10T15:46:21.193 回答
1

作为替代方案,您可以使用数据透视表:

重置索引和格式日期列

df=df.reset_index()
df['date']=pd.to_datetime(df['index'])
df['year']=df['date'].dt.year
df['month']=df['date'].dt.month
df['day']=df['date'].dt.day

透视月和日列:

df_grouped=df.pivot_table(index=('month','day'),values='Date',aggfunc='max')
于 2019-06-10T16:15:17.873 回答
0

为什么不保持简单!

max_temp = dfall.groupby([(dfall.Date.dt.month),(dfall.Date.dt.day)])['Data_Value'].max()
于 2020-09-30T18:18:44.047 回答