试图用一个枢轴表制作一个热图,但我很难按顺序排序我的原始数据帧。下面是一个示例代码,说明了我的数据看上去和我是如何制作枢轴表的。
simple_df = pd.DataFrame({'Skill 1': ['Python','Python','Python','Communication','Communication','Communication','Data Governance','Data Governance','Data Governance'], 'Skill 2': ['Python','Communication','Data Governance','Python','Communication','Data Governance','Python','Communication','Data Governance'],'Score':[1,0.9,0.4,0.9,1,0.4,0.4,0.4,1],'Skill 1 Type':['Programming','Programming','Programming','Written','Written','Written','Cyber','Cyber','Cyber']})
simple_df=simple_df.sort_values(by = ['Skill 1 Type'], ascending = [True], na_position = 'first')
test=simple_df.groupby(['Skill 1','Skill 2'], sort=True)['Score'].sum().unstack('Skill 2')由于我排序的“技能1类型”,我想让枢轴表的y轴标签保持相同的顺序,如何“技能1”出现在我的排序数据。因此,理想情况下,我应该(数据治理,Python,通信)而不是(通信,数据治理,Python)在我的y轴。我有什么办法能做到这一点?谢谢!
发布于 2022-10-26 21:23:21
您可以保存原始顺序,然后使用重新索引来交换列和行。
original_order = simple_df["Skill 1"].unique()
(simple_df.pivot(index="Skill 1", columns="Skill 2", values="Score")
.reindex(index=original_order, columns=original_order))
Skill 2 Data Governance Python Communication
Skill 1
Data Governance 1.0 0.4 0.4
Python 0.4 1.0 0.9
Communication 0.4 0.9 1.0https://stackoverflow.com/questions/74214041
复制相似问题