简体   繁体   English

熊猫:使用交叉表时更改列的顺序

[英]Pandas: change the order of the columns when using crosstab

Pretty simply, I want to change the order of the columns for Panda's crosstab .很简单,我想更改 Panda 的crosstab列的顺序。

Right now, it's in alphabetical order, ie: Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday.现在,它是按字母顺序排列的,即:周五、周一、周六、周日、周四、周二、周三。 I would like it to go in order, ie: Monday, Tuesday, ..., Sunday.我希望它按顺序进行,即:星期一,星期二,...,星期日。

This is for a dataset where I wanted to make a crosstab for the days of the week, and the hour of an occurrence.这是一个数据集,我想为一周中的几天和发生的时间制作一个crosstab

I'm doing this right now:我现在正在这样做:

pd.crosstab(data_2019.HOUR, data_2019.DAY_OF_WEEK)

With the output looking like this:输出如下所示:

DAY_OF_WEEK Friday  Monday  Saturday    Sunday  Thursday    Tuesday Wednesday
HOUR                            
0   204 255 256 260 225 222 192
1   121 111 198 230 116 117 145
2   128 90  217 222 84  111 96

You can create a list with the days of the week, in the required order.您可以按照所需的顺序创建一个包含星期几的列表。 Then you can use .crosstab and change the order of the output of running .crosstab using然后您可以使用.crosstab并使用更改运行.crosstab的输出顺序

Generate crosstab生成crosstab

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
        'Friday', 'Saturday', 'Sunday']

c = pd.crosstab(...)

One option一种选择

Change order of columns produced by crosstab更改crosstab表生成的列的顺序

  • this amounts to simply selecting all the columns, but using a list of weekday names in the order you required, since the crosstab output is a just a normal Pandas DataFrame这相当于简单地选择所有列,但是按照您需要的顺序使用工作日名称列表,因为crosstab输出只是一个普通的 Pandas DataFrame
c = c[days]

Alternatively或者

Use .reindex with axis='columns' and specify the list ( days ) to use to change that index (columns) of the DataFrame使用.reindexaxis='columns'并指定列表( days )用于更改DataFrame的索引(列)

c = c.reindex(days, axis="columns")

It is often that one needs to change order of columns and rows, and for that we need to combine approaches outlined the in answer @edesz provided.通常需要更改列和行的顺序,为此我们需要结合@edesz 提供的答案中概述的方法。

For example:例如:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({"a": ["one", "two", "three", "three"], "b": ["two", "one", "three", "one"]})

In [3]: pd.crosstab(df["a"], df["b"]) # wrong order
Out[3]:
b      one  three  two
a
one      0      0    1
three    1      1    0
two      1      0    0

In [4]: pd.crosstab(df["a"], df["b"]).reindex(["one", "two", "three"])[["one", "two", "three"]] # correct order
Out[4]:
b      one  two  three
a
one      0    1      0
two      1    0      0
three    1    0      1

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

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