在Pandas中对数据框进行排序并绘制条形图

时间:2015-04-22 17:38:25

标签: python sorting pandas

我有结果数据框,我想对结果数据框的索引列进行排序。结果数据帧存储先前执行的所有groupby操作

这是结果数据框的输出。

enter image description here

我想对索引列进行排序。所有列的数据类型都是int64。现在它通过字母表来缩短数据。我想用排序数据绘制条形图。

1 个答案:

答案 0 :(得分:0)

您的索引包含在排序之前首先需要转换的日期/句点的字符串表示。

result['dates'] = [dt.datetime.strptime(d, "%b %Y") for d in result.index]
result.sort('dates', inplace=True)

要删除列,

result.drop('dates', inplace=True, axis=1)

使用Pandas Periods(适用于绘制数据):

result['periods'] = [pd.Period(dt.datetime.strptime(d, "%b %Y"), "M") for d in result.index]

将此设置为索引:

result = result.reset_index().set_index('periods')