Pandas DataFrame data row with sort_values ​​another column

I have a Pandas DataFrame. I want to build two column values ​​with a row, and the bar chart sorts the values ​​by another column.

For example, I want to sort the values ​​in descending order by column a_b(sum of column aand b). Also, xlabel is turning, I want to fix it.

Your help will 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']

df[['a','b']].sort_values(by='a',ascending=False).plot(kind='bar',stacked=True)

enter image description here

+4
source share
2 answers

Arrange the data frame first c, then write down with.

df.sort_values('c', ascending=False)[['a','b']].plot.bar(stacked=True)

enter image description here

+4
source

Fix rotation problem with help rot=0in @piRSquared answer.

df.sort_values('c', ascending=False)[['a','b']].plot.bar(stacked=True, rot=0)

enter image description here

+2
source

Source: https://habr.com/ru/post/1662485/


All Articles