You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已手动设置Pandas透视表索引顺序,如何手动设置列顺序(绘图场景)

How to Manually Set Column Order of a Pandas Pivot Table for Plotting

I have the following code:

from io import StringIO
import pandas as pd
import matplotlib.pyplot as plt
txt = '''Category COLUMN1 COLUMN2 COLUMN3
A 0.5 3 Cat1
B 0.3 5 Cat1
C 0.7 4 Cat1
A 0.4 3 Cat2
B 0.8 5 Cat2
C 0.3 4 Cat2'''
df = pd.read_table(StringIO(txt), sep="\s+")
order = ['Cat2', 'Cat1']
col1 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN1').loc[order].plot(kind='bar')
plt.legend(bbox_to_anchor=(1.3, 0.5))
plt.show()

I've manually set the index order of the pivot table using loc, now I need to manually set the column order to adjust the plotting effect. How should I do this?

Great question! Adjusting the column order of your pivot table is straightforward—you can use a similar explicit selection approach you used for the index, just target the columns instead. Here are two clean ways to do it:

Option 1: Combine column and index ordering in one line

Define your desired column order (e.g., ['C', 'B', 'A'] to reverse the default sequence), then select those columns before applying your index filter:

from io import StringIO
import pandas as pd
import matplotlib.pyplot as plt
txt = '''Category COLUMN1 COLUMN2 COLUMN3
A 0.5 3 Cat1
B 0.3 5 Cat1
C 0.7 4 Cat1
A 0.4 3 Cat2
B 0.8 5 Cat2
C 0.3 4 Cat2'''
df = pd.read_table(StringIO(txt), sep="\s+")
order_index = ['Cat2', 'Cat1']
# Define your custom column order here
order_cols = ['C', 'B', 'A']

# First pick columns in your desired order, then filter rows by index
col1 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN1')[order_cols].loc[order_index].plot(kind='bar')
plt.legend(bbox_to_anchor=(1.3, 0.5))
plt.show()

Option 2: Break into separate steps for readability

If you prefer clearer, modular code, create the pivot table first, then reorder columns and index individually:

from io import StringIO
import pandas as pd
import matplotlib.pyplot as plt
txt = '''Category COLUMN1 COLUMN2 COLUMN3
A 0.5 3 Cat1
B 0.3 5 Cat1
C 0.7 4 Cat1
A 0.4 3 Cat2
B 0.8 5 Cat2
C 0.3 4 Cat2'''
df = pd.read_table(StringIO(txt), sep="\s+")

# Create the base pivot table
pivot_df = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN1')
# Reorder columns to your preference
pivot_df = pivot_df[['C', 'B', 'A']]
# Apply your existing index order
pivot_df = pivot_df.loc[['Cat2', 'Cat1']]
# Generate the plot
col1 = pivot_df.plot(kind='bar')
plt.legend(bbox_to_anchor=(1.3, 0.5))
plt.show()

Both methods work because pandas DataFrames let you select columns in any custom order by passing a list of column names to the bracket operator. The bars in your plot will automatically rearrange to match the column sequence you specify.

内容的提问来源于stack exchange,提问作者BoroBorooooooooooooooooooooooo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:13:58