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

时间:2019-03-23 00:34:02

标签: python python-3.x

enter image description here

这是我通过代码得出的结果

<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="left" title="Tooltip on left">Tooltip on left</button>

看起来不错,但是如何使结果更准确地显示每个区域的详细信息呢?

例如,原始名称是收入水平,列名称包括以下内容:

enter image description here

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

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

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