在pandas 0.18.1中,python 2.7.6:
想象一下,我们有下表:
ID,FROM_YEAR,FROM_MONTH,YEARMONTH,AREA,AREA2
1,2015,1,201501,200,100
1,2015,2,201502,200,100
1,2015,3,201503,200,100
1,2015,4,201504,200,100
1,2015,5,201505,200,100
1,2015,6,201506,200,100
1,2015,7,201507,200,100
1,2015,8,201508,200,100
1,2015,9,201509,200,100
1,2015,10,201510,200,100
1,2015,11,201511,200,100
1,2015,12,201512,200,100
1,2016,1,201601,100,200
1,2016,2,201602,100,200
1,2016,3,201603,100,200
1,2016,4,201604,100,200
1,2016,5,201605,100,200
1,2016,6,201606,100,200
1,2016,7,201607,100,200
1,2016,8,201608,100,200
1,2016,9,201609,100,200
1,2016,10,201610,100,200
1,2016,11,201611,100,200
1,2016,12,201612,100,200
有没有什么办法可以和python pandas中的以下MySQL查询做同样的事情(合并函数可能有用,但有没有办法避免在python pandas中进行昂贵的合并/连接)?
SELECT
ID,
FROM_YEAR,
'A' AS TYPE,
AVG(AREA) AS AREA,
AVG(AREA2) AS AREA2
FROM table GROUP BY ID,FROM_YEAR
UNION ALL
SELECT
ID,
FROM_YEAR,
'B' AS TYPE,
AVG(AREA) AS AREA,
AVG(AREA2) AS AREA2
FROM table GROUP BY ID,FROM_YEAR;
此处的目标是按以下格式获取AREA和AREA2列的日历年平均值:
ID,FROM_YEAR,TYPE,AREA,AREA2
1,2015,A,200,100
1,2016,A,100,200
1,2015,B,200,100
1,2016,B,100,200
任何一位大师能开导吗?
=================================一个扩展的问题=========== ==
谢谢你的回答!我刚刚在一个尾随的12个案例中遇到另一个问题:
期望的输出:
ID,FROM_YEAR,FROM_MONTH,YEARMONTH,AREA,AREA2
1,2015,1,201501,NULL,NULL
1,2015,2,201502,NULL,NULL
1,2015,3,201503,NULL,NULL
1,2015,4,201504,NULL,NULL
1,2015,5,201505,NULL,NULL
1,2015,6,201506,NULL,NULL
1,2015,7,201507,NULL,NULL
1,2015,8,201508,NULL,NULL
1,2015,9,201509,NULL,NULL
1,2015,10,201510,NULL,NULL
1,2015,11,201511,NULL,NULL
1,2015,12,201512,200,100
以下代码
agg=df.groupby(['ID','FROM_YEAR'])[['AREA','AREA2']].rolling(window=12).mean()
只会生成此结果,其中缺少FROM_MONTH和YEARMONTH。
ID,FROM_YEAR,AREA,AREA2
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,NULL,NULL
1,2015,200,100
有人能开导吗?谢谢!