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

标签 python matplotlib pandas

我生成一个数据框。我从中拉出一系列 float ,并将其绘制成直方图。工作正常。

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

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)

我刚开始玩 Pandas。也许我没有找到正确的方法来执行“select * from table where WeightClass = 83”等 sql 等价物?

最佳答案

哦,解决了....传递系列及其值属性。

plt.hist(u83.values)

有点奇怪。

作为回溯——现在我的任何子选择方法都起作用了。只是我传递的是 plt.hist(u83) 而不是 plt.hist(u83.values)....有点蹩脚。

关于python - pandas.Series 在 pyplot.hist 中引发 KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27654702/

相关文章:

python - 使用 python 将标签标记为 LMDB 数据中的矩阵

python - 是什么让 Python 的列表追加方法如此之快?

python - 如何让 Pymacs 在 Ubuntu 中使用 EPD python 工作?

python - 使用 python 在框中绘制数据

python - Matplotlib numpy 矩阵的时间序列图

python - matplotlib 等高线图中的闭合线

pandas - pandas 使用哪种方法计算百分位数?

python - 检查按下的键是PyQt中的字母还是空格

python - Pandas - 带公式的列

if-statement - 在 pandas 中使用 If/Truth 语句