【问题标题】:“None of [Index(['', ''], dtype='object')] are in the [columns]” in plotting a Line Plots using matplotlib“[Index(['', ''], dtype='object')] are in the [columns]” in plotting a Line Plots using matplotlib
【发布时间】:2021-01-12 05:37:18
【问题描述】:

我正在尝试使用Canada Immigration 的数据绘制一条线。我的代码在下面...

import pandas as pd

df_canada = pd.read_excel('Canada.xlsx', sheet_name = 'Canada by Citizenship (2)') 


df_canada.set_index('OdName', inplace = True)

import matplotlib.pyplot as plt

years = list (map(str, range(1980, 2014)))
df_canada['Total'] = df_canada[1980] + df_canada[1981]+ df_canada[1982]+ df_canada[1983]+ df_canada[1984]+ df_canada[1985]+ df_canada[1986]+ df_canada[1987]+ df_canada[1988]+ df_canada[1989]+ df_canada[1990]+ df_canada[1991]+ df_canada[1992]+ df_canada[1993]+ df_canada[1994]+ df_canada[1995]+ df_canada[1996]+ df_canada[1997]+ df_canada[1998]+ df_canada[1999]+ df_canada[2000]+ df_canada[2001]+ df_canada[2002]+ df_canada[2003]+ df_canada[2004]+ df_canada[2005]+ df_canada[2006]+ df_canada[2007]+ df_canada[2008]+ df_canada[2009]+ df_canada[2010]+ df_canada[2011]+ df_canada[2012] + df_canada[2013] 


df_canada.loc['Haiti', years].plot(kind = 'line')

plt.title('Immigration from Albania')
plt.ylabel('Number of Immigrants')
plt.xlabel('years')

plt.show()

但代码出现错误“KeyError:”None of [Index(['1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987 ', '1988',\n '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997',\n '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006',\n '2007', '2008', '2009', '2010', '2011', '2012', '2013'],\n dtype='object')] 在 [index]"" 执行行 "df_canada.loc['Haiti', years].plot(kind = '行')"

谁能帮我更正代码? 提前致谢。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    这是您的问题的有效解决方案。您始终可以使用iloc 通过数字访问列:

    import pandas as pd
    import matplotlib.pyplot as plt
      
    df_canada = pd.read_excel('Canada.xlsx', sheet_name = 'Canada by Citizenship (2)')
    
    #summing all the years in one line instead of typing years individually. Years start from column 9.
    df_canada['total'] = df_canada.iloc[:,9:].sum(axis=1)
    
    #Filtering the data country in standard pandas style and select column 9 to second last column (last column is total)
    df_canada[df_canada['OdName']=='Haiti'].iloc[:,9:-1].T.plot(kind = 'line')
    
    
    plt.title('Immigration from Haiti')
    plt.ylabel('Number of Immigrants')
    plt.xlabel('years')
    plt.legend(['number'])
    plt.show()
    

    输出

    【讨论】:

    • 我不知道为什么这没有得到更多的选票,但是您的解决方案有几十个没有用,终于为我解决了这个问题。谢谢!!
    • @ES 谢谢你的好话,伙计!很高兴我能帮助你。感谢您的支持。编码愉快!
    【解决方案2】:

    您已将列表 years 转换为字符串列表(例如 ["1980", 1981", ...]),但您的数据框中的列似乎是整数年(例如 1980 - 而不是“1980”)。尝试将您的 years 变量更改为 years = list(range(1980, 2014))

    此外,您还可以简化 total 计算。

    import pandas as pd
    
    df_canada = pd.read_excel('Canada.xlsx', sheet_name = 'Canada by Citizenship (2)') 
    
    
    df_canada.set_index('OdName', inplace = True)
    
    import matplotlib.pyplot as plt
    
    # Change years to be integers instead of strings
    years = list(range(1980, 2014)) 
    
    # Simply sum across the years using our list of years instead of manually typing each year
    df_canada['Total'] = df_canada[years].sum(axis=1) 
    
    
    df_canada.loc['Haiti', years].plot(kind = 'line')
    
    plt.title('Immigration from Albania')
    plt.ylabel('Number of Immigrants')
    plt.xlabel('years')
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-27
      • 2020-02-14
      • 2015-11-26
      • 2019-06-27
      • 1970-01-01
      • 2022-12-02
      • 2021-01-08
      • 1970-01-01
      相关资源
      最近更新 更多