Daniel Bourke
Daniel Bourke

Reputation: 406

Plot Pandas row values based on descending order

I've got a dataframe with 1000 rows and a Total row at the bottom:

df.tail()

        22   88   24   11   12  21  99  61  13  44  65  64  62  89  220  63
996      1    2    2    0    0   0   0   0   0   0   0   0   0   0    0   0
997      1    1    0    3    0   0   0   0   0   0   0   0   0   0    0   0
998      1    0    0    0    0   0   0   0   0   0   0   0   0   0    0   0
999      0    0    0    1    0   0   0   0   0   0   0   0   0   0    0   0
Total  289  601  304  360  115  31  54  14  33  13  10  14  11   2   13   8

I'd like to plot the Total row in descending order, or even sort the dataframe columns in descending order based on df.loc['Total'].

Currently I've got:

df_sorted = df.sort_values(by=['Total'], axis=0, ascendin=False)
sorted_plot = df_sorted.loc['Total'].plot(kind='bar', title ="Codes", figsize=(15, 10), legend=True, fontsize=12)
plt.show()

However, I'm getting a KeyError for Total.

I've found resources to sort by columns but not by a row.

Upvotes: 0

Views: 809

Answers (1)

DYZ
DYZ

Reputation: 57033

Change axis=0 to axis=1, because you are sorting columns, not rows.

Upvotes: 2

Related Questions