gpt4 book ai didi

python - Pandas MultiIndex 级别的自定义分组

转载 作者:行者123 更新时间:2023-12-02 18:03:20 25 4
gpt4 key购买 nike

最小可重现示例:

import pandas as pd
data = {('Percent', 'Female'): {'Parents': 26.489226869455006, 'Spouse': 31.875792141951838}, ('Percent', 'Male'): {'Parents': 34.49901768172888, 'Spouse': 20.903732809430256}, ('N', 'Female'): {'Parents': 1578.0, 'Spouse': 1578.0}, ('N', 'Male'): {'Parents': 2545.0, 'Spouse': 2545.0}, ('Count', 'Female'): {'Parents': 418.0, 'Spouse': 503.0}, ('Count', 'Male'): {'Parents': 878.0, 'Spouse': 532.0}}
df = pd.DataFrame.from_dict(data)
df

df:

                    Percent                N                Count
Female Male Female Male Female Male
Parents 26.489227 34.499018 1578.0 2545.0 418.0 878.0
Spouse 31.875792 20.903733 1578.0 2545.0 503.0 532.0

我想按性别而不是按统计数据对我的列进行分组,就像这个所需的输出一样:

                                   Female                             Male  
N Count Percent N Count Percent
Parents 1578.0 418.0 26.48.. 2545.0 878.0 34.49..
Spouse 1578.0 503.0 31.87.. 2545.0 532.0 20.90..

我知道如何使用reorder_levels()更改级别顺序。也就是说,我不仅想更改它们的顺序,还想以不同的方式对它们进行分组,如上面所需的输出所示。

我该怎么做?

最佳答案

交换列级别然后排序

df.swaplevel(axis=1).sort_index(axis=1)

对于自定义排序,请在 level=0 上使用 reindex

order = ['Male', 'Female']
df.swaplevel(axis=1).reindex(order, axis=1, level=0)

        Female                      Male                   
Count N Percent Count N Percent
Parents 418.0 1578.0 26.489227 878.0 2545.0 34.499018
Spouse 503.0 1578.0 31.875792 532.0 2545.0 20.903733

关于python - Pandas MultiIndex 级别的自定义分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73844995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com