gpt4 book ai didi

python - 如何通过python更准确地聚合数据框中的值?

转载 作者:太空宇宙 更新时间:2023-11-03 21:07:27 25 4
gpt4 key购买 nike

enter image description here

这是我用代码得到的结果

il = wb[["region","incomeLevel"]]
count_il = il.groupby("incomeLevel")
count_il.count()

看起来不错,但我怎样才能使其完美,让结果为我提供有关每个区域的更多详细信息?

例如,原始名称为收入水平,列名如下:

enter image description here

结果显示每个区域的值。例如,

           East Asia & pacific      South Asia

High income 7 6

有什么想法吗?

最佳答案

我自己找到了另一种方法来解决问题:

il.groupby(['region', 'incomeLevel'])['region'].count()

它显示了以下结果:

region                      incomeLevel        
East Asia & Pacific High income 14
Low income 1
Lower middle income 13
Upper middle income 10
Europe & Central Asia High income 37
Low income 1
Lower middle income 6
Upper middle income 14
Latin America & Caribbean High income 18
Low income 1
Lower middle income 4
Upper middle income 19
Middle East & North Africa High income 8
Low income 2
Lower middle income 5
Upper middle income 6
North America High income 3
South Asia Low income 2
Lower middle income 5
Upper middle income 1
Sub-Saharan Africa High income 1
Low income 27
Lower middle income 14
Upper middle income 6
Name: region, dtype: int64

关于python - 如何通过python更准确地聚合数据框中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55309473/

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