python - 如何对 DataFrame 中特定列的行进行排序?
问题描述
我有一个有 10 列的大数据框。我想仅针对特定列(两个)对所有行进行排序。例如,如果这是我的数据框
A B C
0 5 1 8
1 8 2 2
2 9 3 3
我希望它只对 A 和 B 进行排序,但对行进行排序,所以答案应该是:
A B C
0 1 5 8
1 2 8 2
2 3 9 3
谢谢你。
解决方案
调用np.sort
该特定的列子切片并使用以下方法将其分配回来loc
:
# df.loc[:, ['A', 'B']] = np.sort(df.loc[:, ['A', 'B']], axis=1)
df.loc[:, ['A', 'B']] = np.sort(df.loc[:, ['A', 'B']])
df
A B C
0 1 5 8
1 2 8 2
2 3 9 3
推荐阅读
- group-by - ABAP中内部表的动态GROUP BY
- php - Laravel 从 Restful API 中的 JSON 响应中删除反斜杠
- json - json解码的存储结果突然消失/观察对象在WatchOS中没有反应
- java - 在 spring-MVC 中从 src/main/resources/.. 读取资源
- heroku - 将 Libraris 安装到 Heorku
- python-3.x - 如何保护 Azure Databricks 中的 Python 代码
- amazon-web-services - 本地 IBM MQ 与 AWS 的集成
- php - 如何删除奇怪的空格 - php
- arrays - 数组到对象数组
- c# - 在 C# 中解压 SQL Server 压缩的 varbinary 数组