Pandas / Seaborn KeyError:' [例子]'不在索引中

时间:2017-05-19 23:16:19

标签: python pandas plot seaborn

提前抱歉格式不佳,新手到python并在此网站上发布。

我使用以下代码获取下面的数据框:

staff=df.loc['Adults in my after school program are nice.':'I like the adults in my after school program.', 'Percent':'Performance']

Prompt                                        Percent        Performance
Adults are nice.                              87             Good
Adults treat me fairly.                       70             Needs Improvement
Adults listen to what I have to say.          90             Excellent
I like the adults.                            80             Good

我试图用Seaborn用这段代码绘制它:

fg = seaborn.FacetGrid(data=df, hue='Performance', aspect=1.61)
fg.map(plt.bar, 'Staff', 'Percent').add_legend()
fg

然而,它总是给我错误

  

KeyError:" ['员工']不在索引"

当我尝试绘图时。我已经使用

对其进行了测试
'Prompt' in staff.index 

返回True

我也试过重置索引,但这似乎也无济于事。有关我的代码可能出错的任何想法?提前谢谢。

1 个答案:

答案 0 :(得分:1)

目前还不清楚你想要从数据框中绘制出什么样的内容,但可能有更简单的方法。

如果它只是一个条形图,你可以只是:

 sns.barplot(data=staff, y='column name you want to associate to y', x='column name you want to associate to x, if any', hue='hue you want to apply if any')

你的代码中有一些错误,因为你是新手,让我帮忙让他们明白:

  • 首先,你制作一个名为staff的新df。之后,你指的是df而不是员工,这可能是一个错误

  • 第二,你是先创建一个网格,然后尝试应用一个地图函数来尝试绘制图形,这不是绘制图形的最常见/最简单的方法

  • 第三,在map函数中,你传递的参数都错了

一个很好的提示是你可以查看一些示例绘图函数,无论是来自seaborn还是matplotlib文档,这里有一些参考:

http://seaborn.pydata.org/generated/seaborn.barplot.html

http://matplotlib.org/examples/api/barchart_demo.html

希望有所帮助