dplyr 中与 top_n() 等效的 Pandas 是什么?
在 R dplyr 0.8.5 中:
> df <- data.frame(x = c(10, 4, 1, 6, 3, 1, 6))
> df %>% top_n(2, wt=x)
x
1 10
2 6
3 6
Run Code Online (Sandbox Code Playgroud)
正如 dplyr 文档所强调的那样,请注意,我们在这里得到了 2 个以上的值,因为有一个关系:top_n() 要么获取所有带有值的行,要么没有。
我在 Pandas 1.0.1 中的尝试:
df = pd.DataFrame({'x': [10, 4, 1, 6, 3, 1, 6]})
df = df.sort_values('x', ascending=False)
df.groupby('x').head(2)
Run Code Online (Sandbox Code Playgroud)
结果:
x
0 10
3 6
6 6
1 4
4 3
2 1
5 1
Run Code Online (Sandbox Code Playgroud)
预期成绩:
x
1 10
2 6
3 6
Run Code Online (Sandbox Code Playgroud)
使用参数keep='all'in DataFrame.nlargest,这里不需要排序:
df = df.nlargest(2, 'x', keep='all')
print(df)
x
0 10
3 6
6 6
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
219 次 |
| 最近记录: |