# 熊猫枢轴多索引列

`````` Shape of passed values is (3, 4), indices imply (3, 2)
``````

``````import pandas as pd

df = pd.DataFrame({
'foo': [1,2,3], 'bar':[4,5,6], 'dt':['2020-01-01', '2020-01-01', '2020-01-02'], 'cat':['a', 'b', 'b']
})
df = df.groupby(['dt', 'cat']).describe().loc[:, pd.IndexSlice[:, ['count', '50%']]].reset_index()
columns_of_interest = sorted(df.drop(['dt', 'cat'], axis=1, level=0).columns.get_level_values(0).unique())
df.pivot(index='dt', columns='cat', values=columns_of_interest)
``````

## 编辑

``````dt  cat     foo     bar
count   50%     count   50%
0   2020-01-01  a   1.0     1.0     1.0     4.0
1   2020-01-01  b   1.0     2.0     1.0     5.0
2   2020-01-02  b   1.0     3.0     1.0     6.0
``````

``````value       foo         bar

cat     a       b       a       b
dt

0
1
2
``````

## 编辑2

``````v = 'count'
df['foo'][v].reset_index().pivot(index='dt', columns='cat', values = v)
``````

``````for c in columns_of_interest:
print(c)
for piv in piv_values:
print(piv)
r = df[c][piv].reset_index().pivot(index='dt', columns='cat', values = piv)
display(r)
``````

1）我只是不确定如何重新组合结果，以及2）如何找到整洁的解决方案。

### 解决方法

``````df.columns = ['_'.join(col).strip() for col in df.columns.values]
columns_of_interest = df.columns
df.reset_index().pivot(index='dt', columns='cat', values=columns_of_interest)
``````

IIUC，您可以`unstack``groupby`（no reset_index）之后使用

``````df = pd.DataFrame({
'foo': [1,2,3], 'bar':[4,5,6],
'dt':['2020-01-01', '2020-01-01', '2020-01-02'], 'cat':['a', 'b', 'b']
})
df_ = df.groupby(['dt', 'cat']).describe()\
.loc[:, pd.IndexSlice[:, ['count', '50%']]]\
.unstack() # unstack instead of reset_index

print (df_)
foo                  bar
count       50%      count       50%
cat            a    b    a    b     a    b    a    b
dt
2020-01-01   1.0  1.0  1.0  2.0   1.0  1.0  4.0  5.0
2020-01-02   NaN  1.0  NaN  3.0   NaN  1.0  NaN  6.0
``````

0 条评论