我应该纠正什么错误?

2024-06-25 22:51:42 发布

您现在位置:Python中文网/ 问答频道 /正文

你能告诉我我犯了什么错误吗

作为pd进口熊猫 将numpy作为np导入

州={'OH':'Ohio','KY':'Kentucky','AS':'American Samoa','NV': “内华达州”、“怀俄明州”、“纳州”、“国家”、“艾尔州”、“阿拉巴马州”、“马里兰州”、“AK州”、“阿拉斯加州”、“UT州”、“犹他州”、“或”、“俄勒冈州”、“蒙特州”、“蒙大拿州”、“伊利诺伊州”、“TN州”、“田纳西州”、“DC州”、“哥伦比亚特区”、“VT州”、“佛蒙特州”、“ID州”、“爱达荷州”、“阿尔州”、“阿肯色州”、“我州”、“缅因州”、“西澳州”、“华盛顿州”、“你好”、“夏威夷州”、“威斯康星州”、“密歇根州”、“密歇根州”在“:”印第安纳州“,”新泽西州“,”亚利桑那州“,”古“,”关岛“,”密西西比州“,”北卡罗来纳州“,”德克萨斯州“,”南达科他州“,”德克萨斯州“,”SD“,”南达科他州“,”国会议员“,”北马里亚纳群岛“,”IA“,”爱荷华州“,”密苏里州“,”康涅狄格州“,”西弗吉尼亚州“,”南卡罗来纳州“,”洛杉矶“,”路易斯安那州“,”堪萨斯州“,”纽约“:”新州“,”纽约州、东北州、内布拉斯加州、俄克拉何马州、佛罗里达州、佛罗里达州、加利福尼亚州、科罗拉多州、宾夕法尼亚州、德州、特拉华州、新墨西哥州、里州、罗德岛、明尼苏达州、明尼苏达州、六州、维尔京群岛、新罕布什尔州、马萨诸塞州、乔治亚州、ND州、北达科他州、弗吉尼亚州、

def convert_housing_data_to_quarters():
    '''Converts the housing data to quarters and returns it as mean
    values in a dataframe. This dataframe should be a dataframe with
    columns for 2000q1 through 2016q3, and should have a multi-index
    in the shape of ["State","RegionName"].

    Note: Quarters are defined in the assignment description, they are
    not arbitrary three month periods.

    The resulting dataframe should have 67 columns, and 10,730 rows.
    '''


    df = pd.read_csv('City_Zhvi_AllHomes.csv', header=0)


    cols_to_keep = ['RegionID', 'RegionName', 'State']
    for i in range(2000, 2017):
        for j in range(1, 13):
            if j <= 9:
                if i == 2016 and j == 9:
                    pass
                else:
                    month_str = '0' + str(j)
            else:
                if i == 2016:
                    pass
                else:
                    month_str = str(j)
            cols_to_keep.append(str(i) + '-' + month_str)
    df = df[cols_to_keep]


    df['State'] = df['State'].replace(states)

    def convert_to_qtr(ym):
        year, month = ym.split('-')
        if month == '01' or month == '02' or month == '03':
            result = year + 'q1'
        elif month == '04' or month == '05' or month == '06':
            result = year + 'q2'
        elif month == '07' or month == '08' or month == '09':
            result = year + 'q3'
        else:
            result = year + 'q4'
        return result


    df_compiled = df.copy().set_index(['State', 'RegionName', 'RegionID']).stack(dropna=False)
    df_compiled = df_compiled.reset_index().rename(columns={'level_3': 'year_month', 0: 'gdp'})
    df_compiled.drop_duplicates(inplace=True)
    df_compiled['quarter'] = df_compiled['year_month'].apply(convert_to_qtr)
    df_compiled = df_compiled.drop('year_month', axis=1)
    result = df_compiled.pivot_table(values='gdp', index=['State', 'RegionName', 'RegionID'], columns='quarter', aggfunc=np.mean)
    result = result.reset_index()
    result = result.drop('RegionID', axis=1)
    #del result.index.name
    result = result.set_index(['State', 'RegionName'])
    return result

convert_housing_data_to_quarters()

这就是我在Python 3.8中遇到的错误:

Traceback (most recent call last):
  File "C:/Users/Esteban Andrino/AppData/Local/Programs/Python/Python38-32/K2 Michi_gan/Week_4/Assignment final_4/AssignmentFINAL222_4.py", line 232, in <module>
    convert_housing_data_to_quarters()
  File "C:/Users/Esteban Andrino/AppData/Local/Programs/Python/Python38-32/K2 Michi_gan/Week_4/Assignment final_4/AssignmentFINAL222_4.py", line 202, in convert_housing_data_to_quarters
    df = df[cols_to_keep]
  File "C:\Users\Esteban Andrino\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\frame.py", line 2806, in __getitem__
    indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
  File "C:\Users\Esteban Andrino\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1552, in _get_listlike_indexer
    self._validate_read_indexer(
  File "C:\Users\Esteban Andrino\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1646, in _validate_read_indexer
    raise KeyError(f"{not_found} not in index")
KeyError: "['2003-08', '2008-11', '2012-11', '2003-10', '2006-01', '2009-06', '2008-06', '2009-08', '2007-05', '2009-11', '2012-01', '2006-11', '2009-12', '2007-07', '2001-03', '2004-07', '2005-06', '2007-04', '2016-08', '2002-12', '2007-10', '2008-10', '2003-01', '2001-02', '2011-07', '2012-09', '2014-11', '2015-07', '2001-08', '2003-11', '2015-02', '2007-02', '2015-12', '2011-01', '2013-04', '2011-06', '2014-02', '2012-10', '2013-01', '2010-09', '2009-09', '2011-12', '2014-03', '2013-06', '2013-12', '2001-06', '2001-12', '2011-10', '2009-01', '2015-01', '2004-09', '2001-10', '2000-06', '2009-03', '2006-05', '2011-11', '2014-07', '2004-03', '2008-12', '2010-11', '2011-02', '2009-05', '2002-09', '2012-02', '2004-08', '2003-02', '2016-02', '2003-12', '2005-01', '2005-09', '2006-04', '2008-03', '2012-12', '2000-03', '2005-02', '2005-08', '2006-07', '2013-08', '2016-05', '2002-03', '2007-08', '2015-11', '2016-06', '2001-01', '2006-09', '2010-06', '2014-01', '2014-10', '2003-07', '2014-06', '2016-07', '2004-04', '2010-02', '2002-08', '2005-12', '2004-12', '2000-04', '2006-08', '2010-08', '2011-03', '2004-02', '2000-12', '2001-04', '2000-11', '2012-05', '2002-05', '2009-07', '2011-04', '2000-10', '2011-09', '2002-02', '2004-10', '2005-05', '2007-01', '2007-03', '2010-01', '2012-06', '2014-08', '2015-03', '2014-09', '2013-02', '2013-10', '2006-06', '2003-05', '2012-08', '2001-11', '2005-10', '2002-01', '2013-03', '2011-05', '2000-07', '2001-09', '2012-03', '2008-04', '2014-04', '2000-09', '2009-10', '2006-03', '2000-02', '2015-08', '2012-07', '2013-11', '2003-09', '2010-12', '2000-08', '2015-05', '2015-06', '2012-04', '2015-09', '2007-12', '2009-04', '2002-07', '2013-07', '2014-05', '2008-05', '2016-01', '2007-11', '2000-05', '2016-04', '2010-04', '2008-02', '2004-05', '2005-11', '2008-09', '2010-10', '2015-04', '2006-12', '2007-09', '2013-09', '2009-02', '2010-07', '2016-03', '2002-06', '2002-11', '2002-04', '2003-06', '2010-05', '2011-08', '2002-10', '2008-08', '2003-03', '2004-11', '2005-04', '2015-10', '2007-06', '2004-06', '2010-03', '2005-03', '2000-01', '2008-01', '2005-07', '2008-07', '2006-10', '2006-02', '2013-05', '2001-05', '2004-01', '2003-04', '2014-12', '2001-07'] not in index"

Tags: ortoinconvertdfdataindexresult
1条回答
网友
1楼 · 发布于 2024-06-25 22:51:42

行中:

df = df[cols_to_keep]

cols_to_keep是一个包含以下内容的列表:['2003-08', '2008-11', '2012-11', '2003-10', '2006-01'.... etc ],它不能很好地用作键。您可能希望逐个使用该列表的元素作为键,而不是整个列表

相关问题 更多 >