考虑以下数据框:
columns = pd.MultiIndex.from_tuples([('datetime', 'yearmonth'), ('datetime', 'month'),
('quantity', 'Q1'),('quantity', 'Q2'),('price','P1'),('price','P2'), ('cost', 'C1')])
data = [[201601,1,47,21,13,54,23],[201601,1,35,53,61,33,33],[201602,2,55,11,25,54,31],
[201602,2,12,13,32,34,21],[201603,3,61,44,25,32,12],[201603,3,41,12,56,23,12]]
rng = ['1/1/2016','1/5/2016','2/1/2016','2/23/2016','3/5/2016','3/7/2016']
index = pd.to_datetime(rng)
df = pd.DataFrame(data,index=index, columns=columns)
datetime quantity price cost
yearmonth month Q1 Q2 P1 P2 C1
2016-01-01 201601 1 47 21 13 54 23
2016-01-05 201601 1 35 53 61 33 33
2016-02-01 201602 2 55 11 25 54 31
2016-02-23 201602 2 12 13 32 34 21
2016-03-05 201603 3 61 44 25 32 12
2016-03-07 201603 3 41 12 56 23 12
我正在尝试创建一个数据透视表,用yearmonth
列汇总数据(获取总和和平均值 - 2个不同的dfs)。最终它应该看起来像这样:
yearmonth 201601 201602 201603
cost C1 56 52 24
price P1 74 57 81
P2 87 88 55
quantity Q1 82 67 102
Q2 74 24 56
我尝试pd.pivot_table(df,columns=('datetime','yearmonth'))
,但收到以下错误:KeyError: 'yearmonth'
当列为MultiIndexed时,如何创建数据透视表?