按日期范围对数据帧进行子集设置

2024-09-27 07:27:18 发布

您现在位置:Python中文网/ 问答频道 /正文

试图通过有效的方式将数据帧按日期范围分开,到目前为止,我只想到:

mask0 = df['Date of survey'].between('2010-01-01', '2010-12-31')
mask1 = df['Date of survey'].between('2011-01-01', '2011-12-31')
mask2 = df['Date of survey'].between('2012-01-01', '2012-12-31')

...
maskn = df['Date of survey'].between('nnnn-01-01', 'nnnn-12-31')

任何想法都将不胜感激! (我将使用掩码对数据帧进行子集划分,并获得每年每个列变量的平均和)


Tags: of数据dfdate方式between子集survey
1条回答
网友
1楼 · 发布于 2024-09-27 07:27:18

这里更好的方法是将^{}按年份与meansum等聚合函数一起使用:

df1 = df.resample('A', on='Date of survey').agg(['mean','sum'])

或使用^{}^{}

df2 = df.groupby(df['Date of survey'].dt.year).agg(['mean','sum'])

相关问题 更多 >

    热门问题