2

For 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)
1
  • Your example is a bit odd because row 1 already completely specifies the order of the result, so the lower-priority sorts on other rows will have no effect. Commented Jan 9, 2016 at 2:53

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

This should sort them in order of a>b>c:

df = pd.DataFrame({'x':[1,1,1,2,3,3], 'y':['a','a','c','b','b','b']})
ct = pd.crosstab(df.x, df.y)

y  a  b  c
x         
1  2  0  1
2  0  1  0
3  0  2  0

sorted_df = pd.DataFrame(ct.sort_values(by = ['a','b','c'], ascending = [False,False,False]))

y  a  b  c
x         
1  2  0  1
3  0  2  0
2  0  1  0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.