更新时间:2022-12-27 15:15:48
目前无法通过直接调用 sort_values
来完成此操作.有关于它的一个公开的错误报告.
通过转置、按列排序,然后再次转置,您仍然可以做得不太好:
>>>ct.T.sort_values([1, 2, 3]).Ty b c aX1 0 1 22 1 0 03 2 0 0For example,
df = pd.DataFrame({'x':[1,1,1,2,3,3], 'y':['a','a','c','b','b','b']})
ct = pd.crosstab(df.x, df.y)
ct
y a b c
x
1 2 0 1
2 0 1 0
3 0 2 0
How do I sort the columns of ct based on the values in row1, row2, and row3 (in that order of priority)?
I've tried the following, neither of which work
ct.sort_values([1, 2, 3], axis=1)
ct.sort_values(['1','2','3'], axis=1)
This cannot currently be done with a direct call to sort_values
. There is an open bug report about it.
You can still do it less nicely by transposing, sorting by columns, then transposing again:
>>> ct.T.sort_values([1, 2, 3]).T
y b c a
x
1 0 1 2
2 1 0 0
3 2 0 0