gpt4 book ai didi

python - 在 pivot 之后为多索引重新排序 pandas 数据帧数据

转载 作者:太空宇宙 更新时间:2023-11-04 00:01:16 27 4
gpt4 key购买 nike

我正在为公共(public)交通数据构建一个分析工具,并希望对 pandas 数据框中的数据重新排序,使用以下示例可以最好地解释这一点:

我的初始数据形状是:

            Population                                GDP per capita
date 2015 2016 2017 2015 2016 2017
country
France 66593366.0 66859768.0 67118648.0 40564.460707 41357.986933 42850.386280
Germany 81686611.0 82348669.0 82695000.0 47810.836011 48943.101805 50638.890964
Italy 60730582.0 60627498.0 60551416.0 36640.115578 38380.172412 39426.940797
Spain 46444832.0 46484062.0 46572028.0 34818.120507 36305.222132 37997.852337

我不想 reshape 数据框,使日期成为顶级索引,而当前较低级别的信息 PopulationGDP per capital 位于较低级别。生成的数据框应如下所示:

            2015                            2016                            2017
date Population GDP per capita Population GDP per capita Population GDP per capita
country
France 66593366.0 40564.460707 66859768.0 41357.986933 67118648.0 42850.386280
Germany 81686611.0 47810.836011 82348669.0 48943.101805 82695000.0 50638.890964
Italy 60730582.0 36640.115578 60627498.0 38380.172412 60551416.0 39426.940797
Spain 46444832.0 34818.120507 46484062.0 36305.222132 46572028.0 37997.852337

如何使用 pandas 实现此目的?我一直在试验 swaplevel 但未能获得预期的结果。

数据帧是通过pivot操作从以下数据中获得的:

       country    date    Population    GDP per capita    GNI per capita

1 Germany 2017 82695000.0 50638.890964 51680.0
2 Germany 2016 82348669.0 48943.101805 49770.0
3 Germany 2015 81686611.0 47810.836011 48690.0
60 Spain 2017 46572028.0 37997.852337 37990.0
61 Spain 2016 46484062.0 36305.222132 36300.0
62 Spain 2015 46444832.0 34818.120507 34740.0
119 France 2017 67118648.0 42850.386280 43790.0
120 France 2016 66859768.0 41357.986933 42020.0
121 France 2015 66593366.0 40564.460707 41100.0
237 Italy 2017 60551416.0 39426.940797 39640.0
238 Italy 2016 60627498.0 38380.172412 38470.0
239 Italy 2015 60730582.0 36640.115578 36440.0

以及以下pivot:

df_p = df_small.pivot(
index='country',
columns='date',
values=['Population', 'GDP per capita'])

最佳答案

交换级别和排序索引,

df_p.columns = df_p.columns.swaplevel(1,0)
df_p = df_p.sort_index(axis = 1)


date 2015 2016 2017
GDP per capita Population GDP per capita Population GDP per capita Population
country
France 40564.460707 66593366.0 41357.986933 66859768.0 42850.386280 67118648.0
Germany 47810.836011 81686611.0 48943.101805 82348669.0 50638.890964 82695000.0
Italy 36640.115578 60730582.0 38380.172412 60627498.0 39426.940797 60551416.0
Spain 34818.120507 46444832.0 36305.222132 46484062.0 37997.852337 46572028.0

关于python - 在 pivot 之后为多索引重新排序 pandas 数据帧数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55620694/

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