running man running man - 1 year ago 186
Python Question

Pandas DataFrame bar plot with sort_values by other column

I have a Pandas DataFrame. I want to plot two columns' values with bar plot, and the bar plot sorts values by the other column.

For example, I want to sort values in descending order by column

(sum of column
In addition, the xlabel is rotated, I want to fix it.

Your help would be appreciated.

import pandas as pd
%matplotlib inline
a = pd.Series([4,8,6,7,8,3,9,7])
b = pd.Series([3,6,8,3,4,6,10,4])
a_b = a+b
df = pd.concat([a,b,a_b],axis=1,join='inner')
df.columns = ['a','b','c']


enter image description here

Answer Source

Sort dataframe first by c then plot with.

df.sort_values('c', ascending=False)[['a','b']]

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download