0

I want to use sort_index to sort value_counts()

The df I have is like this

   a
1 low
2 high
3 vhigh
...

I want to counts columns a and sort them by index of low,med,high,vhigh

df['a'].value_counts()
med   20
high  30
low   10
vhigh 15

If I add sort_index,it will be like this

high  30
low   10
med   20
vhigh 15

That's what I want

low   10
med   20
high  30
vhigh 15
3
  • value_counts by default it will be sort by value in descending order. I think your sample output is not correct. provide the actual data frame Commented May 9, 2018 at 6:52
  • I didn't use sort_index at sample, if I add it. the result will be sort in descending, but it still not the answer what am I expecting Commented May 9, 2018 at 6:54
  • why is vhigh 15 at the bottom? Commented May 9, 2018 at 7:14

1 Answer 1

9

Given the question, this has to be a custom sorting problem -

   value  counts
0    med      20
1   high      30
2    low      10
3  vhigh      15

This is the df you get when you do a value_counts()

Define the value field as a pd.Categorical and define the order -

df['value'] = pd.Categorical(df['value'], ["low", "med", "high", "vhigh"])

Then do a sort -

df.sort_values('value')

Output

   value  counts
2    low      10
0    med      20
1   high      30
3  vhigh      15
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but acually low,med,high,vhigh is the value of one columns
@Haliris yes, do this to the output of the value_counts() output dataframe
Thanks. This is very useful for my application too. Interestingly, you didn't need to pass ordered=True when using pd.Categorical() for this to work. I wonder why.

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.