- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下数据框 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
后,各州根据人口的排序被打乱了。许多其他州都会发生这种情况。有人可以告诉我发生了什么,以及如何在选择前三个值时保持排序后的值不变?
最佳答案
您可以使用 groupby
与 SeriesGroupBy.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
关于python - `nth` 破坏了 pandas 中排序的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41161989/
:nth-child(2) 似乎选择了 child 1 内部的东西。 child 1 和 child 3 工作正常。 好像不涉及tag的类型,几个类似但不同的问题都有。我没有看到问题。 https:/
我想用 javascript 隐藏一个特定的 child : #table-detail > tbody > tr:nth-child(10) 基于另一个特定的前一个 child 的内容: #tabl
在我自学 CSS 的过程中,我遇到了伪选择器 :nth-child() (以及它相关的选择器 :nth-last-child() 和 :nth-of-type())。 我已经对它进行了足够的研究,以了
在自学 CSS 的过程中,我遇到了伪选择器 :nth-child() (以及它的相关选择器 :nth-last-child() 和 :nth-of-type())。 我已经对它进行了足够的研究以理解语
我对 nth-of-type 有点困惑伪类,以及它应该如何工作——尤其是与 nth-child 相比类。 也许我的想法是错误的,但是考虑到这个结构 A B 1 2
我对 nth-of-type 有点困惑伪类,以及它应该如何工作——尤其是与 nth-child 相比类。 也许我的想法是错误的,但是考虑到这个结构 A B 1 2
我对 nth-of-type 有点困惑伪类,以及它应该如何工作——尤其是与 nth-child 相比类。 也许我的想法是错误的,但是考虑到这个结构 A B 1 2
我对 nth-of-type 有点困惑伪类,以及它应该如何工作——尤其是与 nth-child 相比类。 也许我的想法是错误的,但是考虑到这个结构 A B 1 2
我对 nth-of-type 有点困惑伪类,以及它应该如何工作——尤其是与 nth-child 相比类。 也许我的想法是错误的,但是考虑到这个结构 A B 1 2
我想避免在自定义 Wordpress 模板中使用函数或循环来为特定元素显示不同的背景颜色。我的问题是需要更改的容器及其父容器。 每个第 1、4、7 等配置文件类都需要有蓝色背景色。每个第 2、5、8
这个问题在这里已经有了答案: What does a space mean in a CSS selector? i.e. What is the difference between .clas
我有 3 个按钮,对于第二个按钮,我试图为其添加额外的边距,但出于某种原因,nth-child 和 nth-of-type 根本没有改变外观。我想我只是不明白它们是如何工作的,所以如果有人可以传播一点
我对 nth-of-type 有点困惑伪类,以及它应该如何工作——尤其是与 nth-child 相比类。 也许我的想法是错误的,但是考虑到这个结构 A B 1 2
我需要在 x 数量的元素之后插入一个 clearfix div,以便我可以获得格式良好的列。 我已经尝试了 :nth-child 和 :nth-of-type 并且我只在前 x 个项目之后添加了一个
遇到一个我想不通的问题。我正在尝试做的一个简单示例: 在类 .row 下第一个“跨度”的每次出现都以红色突出显示,除了第一次出现,它应该以黄色突出显示。 .row span:nth-of-type(1
什么 CSS 选择器可用于选择父元素中的所有奇数元素,但不一定是兄弟元素? A A A
我正在尝试更改 div 内的奇数 div 的样式。出于某种原因,当 nth-of-type(odd) 在另一个 div 中时,它会影响我的所有 div。这是我的常规 div 和奇数 div 的代码:
这个问题在这里已经有了答案: Can I combine :nth-child() or :nth-of-type() with an arbitrary selector? (9 个回答) Wh
我正在尝试更改 div 内的奇数 div 的样式。出于某种原因,当 nth-of-type(odd) 在另一个 div 中时,它会影响我的所有 div。这是我的常规 div 和奇数 div 的代码:
我有这个 html 代码: CSS: .productWarp .productLine { background-color
我是一名优秀的程序员,十分优秀!