简体   繁体   English

对 Pivot 表的列进行排序 Pandas

[英]Sort Column of Pivot table Pandas

I'm trying to sort the columns of a pivot table in a specific order, not alphabetical.我正在尝试按特定顺序而不是字母顺序对 pivot 表的列进行排序。 In my table, I have the same values on the x and y axis and the values are the similarity between them.在我的表中,我在 x 和 y 轴上有相同的值,这些值是它们之间的相似性。 I have the order of the y axis sorted in a specific order and would like the x axis of values to match, so that the diagonal of values will be all 1.0.我将 y 轴的顺序按特定顺序排序,并希望值的 x 轴匹配,以便值的对角线全部为 1.0。 Below is an example of what I'm doing, with example data下面是我正在做的一个例子,带有示例数据

# Code to create pivot table
pivot=final_df.groupby(['Fruit A','Fruit B'], sort=False)['Similarity Score'].sum().unstack('Fruit B')
Apple苹果 Orange橙子 Mango芒果 Banana香蕉
Orange橙子 0.4 0.4 1 1个 0.6 0.6 0.7 0.7
Mango芒果 0.3 0.3 0.4 0.4 1 1个 0.5 0.5
Apple苹果 1 1个 0.3 0.3 0.6 0.6 0.1 0.1
Banana香蕉 0.4 0.4 0.2 0.2 0.5 0.5 1 1个

Ideally, I want the above table to also say orange, mango, apple, banana (in that order) on the column labels, resulting in 1 throughout the diagonal.理想情况下,我希望上表在列标签上也显示 orange、mango、apple、banana(按此顺序),从而在整个对角线上显示 1。 How can I do this effectively?我怎样才能有效地做到这一点?

One option: use an ordered Categorical for your two fruits columns.一种选择:为您的两个水果列使用有序的Categorical

order = ['Orange', 'Mango', 'Apple', 'Banana']
df['Fruit A'] = pd.Categorical(df['Fruit A'], categories=order, ordered=True)
df['Fruit B'] = pd.Categorical(df['Fruit B'], categories=order, ordered=True)
# then pivot

Second option: reindex :第二个选项: reindex

order = ['Orange', 'Mango', 'Apple', 'Banana']
pivot.reindex(index=order, columns=order)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM