2

I currently have a dataframe like so:

 category     name1      name2     name3     name4
    a          4           34       43         34
    b          5           34       31         523 
    c          234         32        4         12  
    d          34          1        13         19

I am trying to plot bar plot subplots of each row in descending order.

I am currently plotting my subplots like this:

 df.plot(kind='bar', subplots=True, layout=(2,10), figsize=(10,10))

How can I sort the current bar charts in descending order.

Let me know if you have any questions. Thanks

3 Answers 3

2

sort_values

df = pd.DataFrame({
    'category': ['a', 'b', 'c', 'd'],
    'name1': [12, 34, 1234, 78],
    'name2': [473, 16, 8891, 73],
    'name3': [768, 521, 521, 1002],
    'name4': [823, 6742, 5934, 37]
})

  category  name1  name2  name3  name4
0        a     12    473    768    823
1        b     34     16    521   6742
2        c   1234   8891    521   5934
3        d     78     73   1002     37


df.sort_values(by=['name1'])

  category  name1  name2  name3  name4
0        a     12    473    768    823
1        b     34     16    521   6742
3        d     78     73   1002     37
2        c   1234   8891    521   5934

df.sort_values(by=['name1'], ascending=False)

  category  name1  name2  name3  name4
2        c   1234   8891    521   5934
3        d     78     73   1002     37
1        b     34     16    521   6742
0        a     12    473    768    823

df.sort_values(by=['name3', 'name2'], ascending=False)

  category  name1  name2  name3  name4
3        d     78     73   1002     37
0        a     12    473    768    823
2        c   1234   8891    521   5934
1        b     34     16    521   6742

So, depending on which column you want to sort by:

df.sort_values(
    by=['name1'], ascending=False).plot(
    kind='bar', subplots=True, layout=(2,10), figsize=(10,10))
Sign up to request clarification or add additional context in comments.

1 Comment

ascending also accepts lists as inputs as to sort each column in a different manner. E.g. df.sort_values(by=['name3', 'name2'], ascending=[True, False])
0

I hope the following example may illustrate how you can achieve that using panda's sort_values

import pandas as pd
import numpy as np

df = pd.DataFrame({
     'category' : ['a', 'b', 'c', 'd'],
     'name1': [4, 34, 43, 34],
     'name2': [5, 34, 31, 523],
     'name3': [234, 32, 4, 12],
     'name4': [34, 1, 13, 19],
})

df2 = df.sort_values(by=['name1'])

print(df2)

Output of this code is:

  category  name1  name2  name3  name4
0        a      4      5    234     34
1        b     34     34     32      1
3        d     34    523     12     19
2        c     43     31      4     13

Comments

0

To sort each row in descending order use np.sort():

df=df.set_index('category') #if category is already an index skip this
df=pd.DataFrame(abs(np.sort(-df,axis=1)),columns=df.columns,index=df.index)
print(df)

          name1  name2  name3  name4
category                            
a            43     34     34      4
b           523     34     31      5
c           234     32     12      4
d            34     19     13      1

1 Comment

downvoter care to expain? the reason for my answer is because OP states " each row in descending order"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.