简体   繁体   English

Python Pandas:按另一列对 Pivot 表列进行排序

[英]Python Pandas: Sorting Pivot Table column by another column

I am trying to pivot some data in Python pandas package by using the pivot_table feature but as part of this I have a specific, bespoke order that I want to see my columns returned in - determined by a Sort_Order field which is already in the dataframe. So for test example with:我正在尝试使用数据透视表功能 pivot Python pandas package 中的一些数据,但作为其中的一部分,我有一个特定的定制顺序,我希望看到我返回的列 - 由已经在 dataframe 中的 Sort_Order 字段确定。所以对于测试示例:


raw_data = {'Support_Reason' : ['LD', 'Mental Health', 'LD', 'Mental Health', 'LD', 'Physical', 'LD'],
            'Setting' : ['Nursing', 'Nursing', 'Residential', 'Residential', 'Community', 'Prison', 'Residential'],
            'Setting_Order' : [1, 1, 2, 2, 3, 4, 2],
            'Patient_ID' : [6789, 1234, 4567, 5678, 7890, 1235, 3456]}

Data = pd.DataFrame(raw_data, columns = ['Support_Reason', 'Setting', 'Setting_Order', 'Patient_ID'])

Data

Then pivot:然后是pivot:

pivot = pd.pivot_table(Data, values='Patient_ID', index=['Support_Reason'],
                   columns=['Setting'], aggfunc='count',dropna = False)
pivot  = pivot.reset_index()

pivot

This is exactly how I want my table to look except that the columns have defaulted to AZ ordering.这正是我希望我的表看起来的样子,只是列默认为 AZ 排序。 I would like them to be ordered Ascending as per the Setting_Order column - so that would be order of Nursing, Residential, Community then Prison.我希望按照 Setting_Order 列对它们进行升序排序——这样顺序是护理、住宅、社区然后是监狱。 Is there some additional syntax that I could add to my pd.pivot_table code would make this possible please?是否有一些额外的语法可以添加到我的 pd.pivot_table 代码中来实现这一点?

I realise there are a few different work-arounds for this, the simplest being re-ordering the columns afterwards(.) but I want to avoid having to hard-code column names as these will change over time (both the headings and their order) and the Setting and Setting_Order fields will be managed in a separate reference table.我意识到有几种不同的解决方法,最简单的是之后重新排序列(。)但我想避免必须对列名进行硬编码,因为这些名称会随着时间而改变(标题和它们的顺序) ) 并且 Setting 和 Setting_Order 字段将在单独的参考表中进行管理。 So any form of answer that will avoid having to list Settings in code would be ideal really.因此,任何形式的避免必须在代码中列出设置的答案都是理想的。

col_order = list(Data.sort_values('Setting_Order')['Setting'].unique())
pivot[col_order+['Support_Reason']]

Does this help?这有帮助吗?

Try:尝试:

ordered = df.sort_values("Setting_Order")["Setting"].drop_duplicates().tolist()
pivot = pivot[list(pivot.columns.difference(ordered))+ordered]

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

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