我有一个10列的大型DataFrame。我只想为特定的列(两列)对所有行进行排序。例如,如果这是我的数据框
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
谢谢。
答案 0 :(得分:2)
在该特定列的子切片上调用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
答案 1 :(得分:1)
我正在使用dummy_array<int, 6> arr{};
sort