阿泽爱学习enen 发表于 2021-12-19 14:16:37

大佬们看看为啥它的错误在哪

代码
import pandas as pd
import matplotlib.pyplot as plt
import os




def dataVisualization():
    fileName=input('请输入要打开的文件名movies_revenue_starring_1950_2010.txt:')
         
    df_tem3=pd.read_csv(fileName,encoding='cp936')
    df_tem3.sort_values(by='Title',ascending=False)#按列对数据进行降序排列
    df_tem3=df_tem3.loc[:,['Revenue','Starring Actors Popularity']]

    #利用matplotlib绘图
    plt.figure(figsize=(12,8))
    plt.plot(df_tem3['Title'],df_tem3['Revenue'],label=\
                      'Revenue',color='red')
    plt.xlabel("Title",fontsize=12)
    #姓名每隔10个抽样显示
    xlength=len(df_tem3)
    print('xlength=',xlength)
    #构建xticks显示位置
    xticksloc=
    print('xticksloc=',xticksloc)
    #构建xticks显示标签
    xtickslabels=df_tem3['Title'].values[::10]
    print('xtickslabels',xtickslabels)
    plt.xticks(xticksloc,xtickslabels,rotation=45)#倾斜45度显示
    plt.legend(fontsize=15)#显示图例并设置字号
    plt.savefig('movies_revenue_1950_2010.png',dpi=400)
    plt.show()
            
    plt.figure(figsize=(12,8))
    plt.plot(df_tem3['Title'],df_tem3['Starring Actors Popularity'],label=\
                     ' Starring Actors Popularity',color='green')
    plt.xlabel("Title",fontsize=12)
    #姓名每隔10个抽样显示
    xlength=len(df_tem3)
    print('xlength=',xlength)
    #构建xticks显示位置
    xticksloc=
    print('xticksloc=',xticksloc)
    #构建xticks显示标签
    xtickslabels=df_tem3['Title'].values[::10]
    print('xtickslabels',xtickslabels)
    plt.xticks(xticksloc,xtickslabels,rotation=45)#倾斜45度显示
    plt.legend(fontsize=15)#显示图例并设置字号
    plt.savefig('movies_starpopularity_1950_2010.png',dpi=400)
    plt.show()
    print('任务5执行成功!')
print(dataVisualization())
            



报错

Traceback (most recent call last):
File "C:\Users\阿泽\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
    return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Title'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:/Users/阿泽/AppData/Local/Programs/Python/Python39/fz1.py", line 52, in <module>
    print(dataVisualization())
File "C:/Users/阿泽/AppData/Local/Programs/Python/Python39/fz1.py", line 17, in dataVisualization
    plt.plot(df_tem3['Title'],df_tem3['Revenue'],label=\
File "C:\Users\阿泽\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 3458, in __getitem__
    indexer = self.columns.get_loc(key)
File "C:\Users\阿泽\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
    raise KeyError(key) from err

suchocolate 发表于 2021-12-19 14:38:34

报错不全,报错贴全。

阿奇_o 发表于 2021-12-19 15:34:36

基本的报错 KeyError ! 自己检查 df_tem3 的列名对不对,到底是不是 'Title' ?

ps: 要自己学会看报错信息啊~
页: [1]
查看完整版本: 大佬们看看为啥它的错误在哪