带有其他列的sort_values的Pandas DataFrame条形图 [英] Pandas DataFrame bar plot with sort_values by other column

查看:112
本文介绍了带有其他列的sort_values的Pandas DataFrame条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Pandas DataFrame.我想用条形图绘制两列的值,然后条形图按另一列对值进行排序.

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.

例如,我想按列a_b(列ab的总和)以降序对值进行排序. 另外,xlabel旋转了,我想修复它.

For example, I want to sort values in descending order by column a_b(sum of column a and b). In addition, the xlabel is rotated, I want to fix it.

您的帮助将不胜感激.

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)

推荐答案

先按c对数据框进行排序,然后进行绘制.

Sort dataframe first by c then plot with.

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

这篇关于带有其他列的sort_values的Pandas DataFrame条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆