如何根据类别列表更改列顺序?



我有一个数据框df,列cats是分类的,如:

> df['cats'] = pd.Categorical(df['cats'], categories=['A+', 'A', 'B+', 'B', 'C', '-'], ordered=True)

df我得到一个df1如下表所示:

value_name-0.04group10.02+0.12B +0.25-0.000.05+0.30B +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.pivotDataFrame.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

最新更新