【问题标题】:`nth` breaks the sorted dataframe in pandas`nth` 破坏了 pandas 中的排序数据框
【发布时间】:2017-04-30 22:15:52
【问题描述】:

我有以下数据框 census_df,其中包含美国的人口数据:

         STNAME             CTYNAME  CENSUS2010POP
0       Alabama      Autauga County          54571
1       Alabama      Baldwin County         182265
2       Alabama      Barbour County          27457
3       Alabama         Bibb County          22915
4       Alabama       Blount County          57322
5       Alabama      Bullock County          10914
6       Alabama       Butler County          20947
7       Alabama      Calhoun County         118572
8       Alabama     Chambers County          34215
9       Alabama     Cherokee County          25989
10      Alabama      Chilton County          43643
11      Alabama      Choctaw County          13859
12      Alabama       Clarke County          25833
13      Alabama         Clay County          13932
14      Alabama     Cleburne County          14972
15      Alabama       Coffee County          49948
16      Alabama      Colbert County          54428
17      Alabama      Conecuh County          13228
18      Alabama        Coosa County          11539
19      Alabama    Covington County          37765
20      Alabama     Crenshaw County          13906
21      Alabama      Cullman County          80406
22      Alabama         Dale County          50251
23      Alabama       Dallas County          43820
24      Alabama       DeKalb County          71109
25      Alabama       Elmore County          79303
26      Alabama     Escambia County          38319
27      Alabama       Etowah County         104430
28      Alabama      Fayette County          17241
29      Alabama     Franklin County          31704
...         ...                 ...            ...
3112  Wisconsin     Washburn County          15911
3113  Wisconsin   Washington County         131887
3114  Wisconsin     Waukesha County         389891
3115  Wisconsin      Waupaca County          52410
3116  Wisconsin     Waushara County          24496
3117  Wisconsin    Winnebago County         166994
3118  Wisconsin         Wood County          74749
3119    Wyoming       Albany County          36299
3120    Wyoming     Big Horn County          11668
3121    Wyoming     Campbell County          46133
3122    Wyoming       Carbon County          15885
3123    Wyoming     Converse County          13833
3124    Wyoming        Crook County           7083
3125    Wyoming      Fremont County          40123
3126    Wyoming       Goshen County          13249
3127    Wyoming  Hot Springs County           4812
3128    Wyoming      Johnson County           8569
3129    Wyoming      Laramie County          91738
3130    Wyoming      Lincoln County          18106
3131    Wyoming      Natrona County          75450
3132    Wyoming     Niobrara County           2484
3133    Wyoming         Park County          28205
3134    Wyoming       Platte County           8667
3135    Wyoming     Sheridan County          29116
3136    Wyoming     Sublette County          10247
3137    Wyoming   Sweetwater County          43806
3138    Wyoming        Teton County          21294
3139    Wyoming        Uinta County          21118
3140    Wyoming     Washakie County           8533
3141    Wyoming       Weston County           7208

[3142 rows x 3 columns]

这些列代表州名、县名和人口。现在,我试图找出每个州人口最多的三个县,然后我想将他们的人口相加,这样我就可以得到每个州的数字。为了获得每个州人口最多的县,我尝试了以下操作:

'''Sort all the counties according to their population'''
census_df = census_df.sort_values(by = 'CENSUS2010POP', ascending = False).reset_index(drop = True)

'''Group counties according to their states and choose first 3 members from each state'''
group = census_df.groupby('STNAME').nth([0, 1, 2])
print(group.tail())

这给了我以下信息(我只显示最后几个值):

           CENSUS2010POP          CTYNAME
STNAME                                   
Wisconsin         488073      Dane County
Wisconsin         389891  Waukesha County
Wyoming            91738   Laramie County
Wyoming            46133  Campbell County
Wyoming            75450   Natrona County

如您所见,对于最后一个状态Wyoming,在使用nth 后,根据人口对状态的排序受到了干扰。这发生在许多其他州。有人能告诉我发生了什么吗?在选择前三个时如何保持排序值不变?

【问题讨论】:

    标签: python python-3.x sorting pandas group-by


    【解决方案1】:

    您可以将groupbySeriesGroupBy.nlargest 一起使用,比.sort_values(ascending=False).head(n) 更快:

    print (census_df.set_index('CTYNAME')
                    .groupby('STNAME')['CENSUS2010POP']
                    .nlargest(3)
                    .sort_index(ascending=False)
                    .reset_index())
    
          STNAME            CTYNAME  CENSUS2010POP
    0    Wyoming     Natrona County          75450
    1    Wyoming     Laramie County          91738
    2    Wyoming    Campbell County          46133
    3  Wisconsin   Winnebago County         166994
    4  Wisconsin    Waukesha County         389891
    5  Wisconsin  Washington County         131887
    6    Alabama      Etowah County         104430
    7    Alabama     Calhoun County         118572
    8    Alabama     Baldwin County         182265
    

    3 最高值的总和:

    print (census_df.set_index('CTYNAME')
                    .groupby('STNAME')['CENSUS2010POP']
                    .apply(lambda x: x.nlargest(3).sum())
                    .sort_index(ascending=False)
                    .reset_index())
    
          STNAME  CENSUS2010POP
    0    Wyoming         213321
    1  Wisconsin         688772
    2    Alabama         405267
    

    【讨论】:

    • 我添加了 3 个最高值之和的答案,您可以检查一下。谢谢。
    • 这很有帮助。谢谢+1
    【解决方案2】:

    我相信你想这样做:

    group = census_df.groupby('STNAME').head(3)
    

    这将返回每组的前 3 行。

    要获得每个状态的总和,只需在您的组上运行 groupby 和 sum aggregate 函数:

    summed = group.groupby('STNAME').aggregate(sum)
    

    【讨论】:

    • 这会破坏groupby。它给了我每个州的 3 个结果,但所有州的前三个县都是混合的。
    • 在您的排序中,运行census_df = census_df.sort_values(by = ['STNAME','CENSUS2010POP'], ascending = False).reset_index(drop = True)。然后运行group = census_df.groupby('STNAME').head(3)
    • 是的,这确实有效!现在,我如何将前三个值相加并得到每个状态的单个值?
    • 另外,在 sort_values 中使用列表后发生了什么神奇的事情?
    • 首先按STNAME 排序可确保将您的状态保持在一起。现在总结一下,您可以在第一个 groupby 之后使用聚合函数链接另一个 groupbygroup = census_df.groupby('STNAME').head(3).groupby('STNAME').aggregate(sum)
    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 2016-01-21
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 2016-03-06
    相关资源
    最近更新 更多