Python 大熊猫中条形图的替换标签

Python 大熊猫中条形图的替换标签,python,pandas,bar-chart,Python,Pandas,Bar Chart,我有一个pandas数据框,我在一个特定列(“问题1”)上绘制条形图,使用: 这很有效。value_counts()方法返回以下序列: 4 30 3 20 5 15 2 10 然后,第一列用作钢筋的标签,第二列为钢筋高度 现在,我有了另一个数据帧df2,它对第一个数据帧df1的问题_1列中的值进行了编码: ID value 1 "test1" 2 "test2" 3 "test3" 4 "test4" 5 "test5" 现在我想对值_counts()的结果使用

我有一个pandas数据框,我在一个特定列(“问题1”)上绘制条形图,使用:

这很有效。value_counts()方法返回以下序列:

4  30
3  20
5  15
2  10
然后,第一列用作钢筋的标签,第二列为钢筋高度

现在,我有了另一个数据帧df2,它对第一个数据帧df1的问题_1列中的值进行了编码:

ID  value
1   "test1"
2   "test2"
3   "test3"
4   "test4"
5   "test5"
现在我想对值_counts()的结果使用这种编码:

最后,我的目标是用这些编码替换条形图的标签(即,我希望使用“test4”作为标签而不是4)。也许这也可以用一种更简单的方法来完成。

IIUC

您可以将值计数产生的序列索引映射到新索引:

s是值_计数的结果:

"test4"  30
"test3"  20
"test5"  15
"test2"  10
s = pd.Series([30,20,15,10],index=[4,3,5,2])
和绘图:

df2是您的“编码数据帧”:

让我们使用以下方法将s.index映射到df2值:

s.index = s.index.to_series().map(df2.set_index('ID')['value'])
现在就开始

s.plot(kind='bar', rot=0)
输出:

s.index = s.index.to_series().map(df2.set_index('ID')['value'])
s.plot(kind='bar', rot=0)