我有一个具有两个维度region
和的熊猫数据框架products
,以及两个度量cost
和price
:
df = pd.DataFrame(
{'region':['N', 'S', 'W', 'E', 'N', 'S', 'W', 'E'],
'product':['P1', 'P1', 'P1', 'P1', 'P2', 'P2', 'P2', 'P2'],
'cost':[10, 13, 17, 28, 29, 23, 17, 18],
'price':[7, 8, 4, 11, 9, 13, 7, 8]})
我想获得:
region E N S W
price cost price cost price cost price cost
product
P1 11 28 ...
P2 8 18 ...
我试过了:
df1 = df.groupby(['product', 'region'])
.agg({'price': 'first', 'cost': 'first'})
.unstack('region')
.swaplevel(axis=1)
print(df1)
但是我得到:
region E N S W E N S W
price price price price cost cost cost cost
product
P1 11 7 8 4 28 10 13 17
P2 8 9 13 7 18 29 23 17
我想念什么?
reindex
按第一级的MultiIndex
in列添加:
df1 = (df.groupby(['product', 'region'])
.agg({'price': 'first', 'cost': 'first'})
.unstack('region')
.swaplevel(axis=1)
.reindex(columns=['E','N','S','W'], level=0))
print(df1)
region E N S W
price cost price cost price cost price cost
product
P1 11 28 7 10 8 13 4 17
P2 8 18 9 29 13 23 7 17
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句