具有正确的列值[重复项]的键错误

问题描述 投票:0回答:3
我对编程还比较陌生,我正在尝试执行我的代码以绘制值

我的代码:

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文件看起来像这样:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9zaW54bS5wbmcifQ==” alt =“在此处输入图像描述”>

非常感谢您的帮助

python pandas dataframe keyerror
3个回答
1
投票
修复列名

  • pandas.Series.str.strip

  • 0
    投票
    请尝试一下,

    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)


    -1
    投票
    我认为您在这一行中写错了: plt.plot(df[df.columns[0]], df[df.columns[1]]) ,应该是plt.show

    此代码运行良好:

    plt.show()

    © www.soinside.com 2019 - 2024. All rights reserved.