python - 具有正确列值时出现键错误
问题描述
我对编程比较陌生,我正在尝试执行我的一段代码以绘制值
我的代码:
import matplotlib.pyplot as plt
import pandas as pd
# set directory
df = pd.read_excel('Angle difference plot.xlsx', 'Sheet1')
# set plot
plt.plot(df['Angle'], df['RE Angle'])
# set label
plt.xlabel('calculated angle')
plt.ylabel('ground truth)')
plt.title('angle accuracy plot')
plt.legend()
plt.show
当我执行上面的代码时,我得到以下错误:
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Angle'
有人能解释一下为什么我会看到这个错误吗?
我的 excel 文件如下所示:
很感谢任何形式的帮助
解决方案
修复列名
pandas.Series.str.strip
- 删除前导和尾随空格。
import pandas as pd
import matplotlib.pyplot as plt
# set directory
df = pd.read_excel('Angle difference plot.xlsx', 'Sheet1')
# clear whitespace from the beginning and end of the column names
df.columns = df.columns.str.strip()
# plot
plt.plot('Angle', 'RE Angle', data=df)
推荐阅读
- python - 如何在两个日期之间的对象列表中搜索值python
- arrays - go语言中如何将带有map的struct存储到数组中
- python - 如何按日期范围汇总总和并加入
- java - 想要制作从动态创建的editTexts中获取值的按钮
- angular - 为什么按键事件在两个输入中不起作用?
- javascript - 在 v-for 中绑定不同的事件监听器
- android - 更改 LiveData 时 DataBinding 不更新值
- selenium - Python selenium 将表值获取到列表列表中
- php - 创建 Laravel 项目
- javascript - commonjs和es6模块的去重