简体   繁体   English

如何排列 pandas pivot 表列?

[英]How to arrange pandas pivot table columns?

How do I arrange the columns of a pandas pivot table?如何排列 pandas pivot 表的列?

For example for the pivot output, how can I have D and E swap positions and how can I swap the large and small positions?以pivot output为例,D和E如何调换仓位,大小仓怎么调换?

EDIT: I am looking for a solution which allows for a custom order that would work for other instances编辑:我正在寻找一种解决方案,该解决方案允许适用于其他实例的自定义订单

#Sample Dataframe
df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                         "bar", "bar", "bar", "bar"],
                   "B": ["one", "one", "one", "two", "two",
                         "one", "one", "two", "two"],
                   "C": ["small", "large", "large", "small",
                         "small", "large", "small", "small",
                         "large"],
                   "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
                   "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
#Dataframe:
    A   B   C       D   E
0   foo one small   1   2
1   foo one large   2   4
2   foo one large   2   5
3   foo two small   3   5
4   foo two small   3   6
5   bar one large   4   6
6   bar one small   5   8
7   bar two small   6   9
8   bar two large   7   9
#Code used to pivot
display(pd.pivot_table(df,index=['A','B'],columns=['C'],values=['D','E'],aggfunc=np.sum))
Pivot table output:
                    D               E
    C   large   small   large   small
A   B               
bar one 4.0     5.0     6.0     8.0
    two 7.0     6.0     9.0     9.0
foo one 4.0     1.0     9.0     2.0
    two NaN     6.0     NaN     11.0

You can define yourself the codes attribute of a MultiIndex instance (like an ordered CategoricalIndex )您可以自己定义MultiIndex实例的codes属性(如有序的CategoricalIndex

out.columns = out.columns.set_codes([
    out.columns.get_level_values(0).map({'E': 0, 'D': 1}),
    out.columns.get_level_values(1).map({'small': 0, 'large': 1})
])
print(out)

# Output
            D           E      
C       small large small large
A   B                          
bar one   4.0   5.0   6.0   8.0
    two   7.0   6.0   9.0   9.0
foo one   4.0   1.0   9.0   2.0
    two   NaN   6.0   NaN  11.0

You can define a custom mapping for month like this:您可以像这样定义月份的自定义映射:

import calendar
month_codes = dict(enumerate(calendar.month_abbr[1:]))
print(month_codes)

# Output
{0: 'Jan', 1: 'Feb', 2: 'Mar', 3: 'Apr', 4: 'May', 5: 'Jun',
 6: 'Jul', 7: 'Aug', 8: 'Sep', 9: 'Oct', 10: 'Nov', 11: 'Dec'}

Is this what you are looking to do?这是你想要做的吗?

pivot1 = pd.pivot_table(df,index=['A','B'],columns=['C'], values=['D','E'],aggfunc=np.sum)

pivot2 = pivot1[[('E', 'small'),
                ('E', 'large'),
                ('D', 'small'),
                ('D', 'large')]]

在此处输入图像描述

You can use sort_index with ascending=False in this example:在此示例中,您可以将sort_indexascending=False一起使用:

df.sort_index(1, ascending=False)

Output Output

            E           D      
C       small large small large
A   B                          
bar one   8.0   6.0   5.0   4.0
    two   9.0   9.0   6.0   7.0
foo one   2.0   9.0   1.0   4.0
    two  11.0   NaN   6.0   NaN

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

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