简体   繁体   中英

Sort Column of Pivot table Pandas

I'm trying to sort the columns of a pivot table in a specific order, not alphabetical. In my table, I have the same values on the x and y axis and the values are the similarity between them. 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. 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 1 0.6 0.7
Mango 0.3 0.4 1 0.5
Apple 1 0.3 0.6 0.1
Banana 0.4 0.2 0.5 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. How can I do this effectively?

One option: use an ordered Categorical for your two fruits columns.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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