[英]Pandas: how to aggregate data weekly?
I have a pandas dataframe the looks like the following:我有一个 pandas dataframe 如下所示:
df
date lat lon val
0 2010-09-01 38.5437 -9.50659 6
1 2010-09-02 38.5437 -9.50659 3
2 2010-08-10 38.5437 -9.50659 1
3 2010-08-11 38.5437 -9.50659 5
4 2010-08-12 38.5437 -9.50659 6
I would like, for each values of lat
and lon
, to have the average value of val
by week.对于
lat
和lon
的每个值,我希望按周获得val
的平均值。
For instance I would like something like the following:例如,我想要以下内容:
df
month week lat lon val
0 2010-09 1 38.5437 -9.50659 4.5
4 2010-08 2 38.5437 -9.50659 4
This is what I am trying to do as a first step but I get an error这是我试图做的第一步,但我得到一个错误
df = df.resample('W', on='date')['val'].mean().reset_index(drop=True)
DataError: No numeric types to aggregate
or或者
df = df.groupby([['lat', 'lon'], pd.Grouper(key='date', freq='W-MON')])['val'].mean().reset_index()
ValueError: Grouper and axis must be same length
Convert val
to numeric first and then remove []
around 'lat', 'lon'
:首先将
val
转换为数字,然后删除'lat', 'lon'
周围的[]
:
df['val'] = pd.to_numeric(df['val'])
df['date'] = pd.to_datetime(df['date'])
df = (df.groupby(['lat', 'lon', pd.Grouper(key='date', freq='W-MON')])['val']
.mean()
.reset_index())
print (df)
lat lon date val
0 38.5437 -9.50659 2010-08-16 4.0
1 38.5437 -9.50659 2010-09-06 4.5
If need month periods and week of year:如果需要月份和一年中的一周:
df = df.groupby([df['date'].dt.to_period('m').rename('month'),
df['date'].dt.isocalendar().week.rename('week'),
'lat', 'lon'])['val'].mean().reset_index()
print (df)
month week lat lon val
0 2010-08 32 38.5437 -9.50659 4.0
1 2010-09 35 38.5437 -9.50659 4.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.