python – pandas.Series在pyplot.hist中引发KeyError

我生成一个Dataframe.我从中拉出一系列花车,然后用直方图绘制它.工作良好.

但是,当我生成该数据的子系列时,使用以下两种描述之一:

u83 = results['Wilks'][results['Weight Class'] == 83]
u83 = results[results['Weight Class'] == 83]['Wilks']

pyplot.hist在该系列上抛出KeyError.

#this works fine
plt.hist(results['Wilks'], bins=bins)
# type is <class 'pandas.core.series.Series'>
print(type(results['Wilks']))
# type is <type 'numpy.float64'>
print(type(results['Wilks'][0]))

#this histogram fails with a KeyError for both of these selectors:
u83 = results['Wilks'][results['Weight Class'] == 83]
u83 = results[results['Weight Class'] == 83]['Wilks']
print u83
#type is <class 'pandas.core.series.Series'>
print(type(u83))
#plt.hist(u83) fails with a KeyError
plt.hist(u83)

我刚刚开始搞乱熊猫.也许我没有正确的方法来做一个sql相当于’select * from table of WeightClass = 83’等?

最佳答案 哦,解决了….传递具有其values属性的Series.

plt.hist(u83.values)

有点奇怪.

作为回溯 – 现在我的任何子选择方法都有效.简直就是我通过了plt.hist(u83)而不是plt.hist(u83.values)….蹩脚的.

点赞