我有一个数据框df
,列cats
是分类的,如:
> df['cats'] = pd.Categorical(df['cats'], categories=['A+', 'A', 'B+', 'B', 'C', '-'], ordered=True)
从df
我得到一个df1
如下表所示:
value_name… - 0.04 … group10.02 …+ 0.12 …B + 0.25 …- 0.00 … 0.05 …+ 0.30 …B + 0.04 …- 0.13 …………… …
您的pivot
似乎不正确,您应该修复参数并与axis=1
上的sort_index
结合:
df2 = (df
.pivot(index='groups', columns='cats', values='value_name')
.sort_index(axis=1)
)
输出:
cats A+ A B+ B -
groups
group1 0.12 0.02 0.25 0.00 0.04
group2 0.30 0.05 0.04 0.09 0.00
group3 NaN NaN NaN NaN 0.13
你可以检查你有一个有序的CategoricalIndex作为列:
df2.columns
CategoricalIndex(['A+', 'A', 'B+', 'B', '-'],
categories=['A+', 'A', 'B+', 'B', 'C', '-'],
ordered=True, dtype='category', name='cats')
对于我使用DataFrame.pivot
和DataFrame.sort_index
来正确排序列:
df['cats'] = pd.Categorical(df['cats'],
categories=['A+', 'A', 'B+', 'B', 'C', '-'],
ordered=True)
df2 = df.pivot('groups','cats','value_name').sort_index(axis=1)
print (df2)
cats A+ A B+ B -
groups
group1 0.12 0.02 0.25 0.00 0.04
group2 0.30 0.05 0.04 0.09 0.00
group3 NaN NaN NaN NaN 0.13