Python 使用Pandas在面板数据中创建月平均数据到季度数据

Python 使用Pandas在面板数据中创建月平均数据到季度数据,python,pandas,converters,panel-data,Python,Pandas,Converters,Panel Data,我得到了以下数据集: Date Country Specie Monthly Average \ Apr 2015 BR co 5.840000 Apr 2015 BR no2 7.553704 Apr 2015 BR o3 15.561667 Apr 2015 BR

我得到了以下数据集:

     Date Country         Specie  Monthly Average  \
 Apr 2015       BR             co         5.840000   
 Apr 2015       BR            no2         7.553704   
 Apr 2015       BR             o3        15.561667   
 Apr 2015       BR           pm10        16.283333   
 Apr 2015       BR           pm25        51.633333   

对于10个国家,2015年至2021年的特定排放量(种类)。我想将其转换为以下形式的季度平均数据(使用一个季度中相应月份的平均值)作为示例:

     Date Country         Specie  Quarterly Average  \
 2015 Q1       BR             co         6.840000   
 2015 Q1       BR            no2         9.553704   
 2015 Q1       BR             o3        17.561667   
 2015 Q1       BR           pm10        18.283333   
 2015 Q1       BR           pm25        55.633333   

如何在python中实现这一点

另外,我还有一个问题,如果我想在列中对物种进行分离,并获取相应的值,如何才能获得以下结构:

     Date Country         co Average       no2 Average   o3 Average      ...    \
 2015 Q1       BR           6.840000      9.553704   17.561667 
 2015 Q2       BR           8.840000      10.553704   18.561667 

首先从原始的
日期
列创建一个
季度

df['Quarter']=pd.to_datetime(df['Date']).dt.to_period('Q')
然后按
国家
品种
季度
列分组,计算各组
月平均
列的平均值。将结果列重命名为
季度平均值

df\=df.groupby(['Country'、'Specie'、'quartery'],as\'index=False)['Monthly Average'].mean()。重命名(列={'Monthly Average':'Quarterly Average'})

仅供参考,这是您想要获得的结构。

非常感谢!