【问题标题】:KeyError: "['month' 'day'] not in index"KeyError:“['month''day'] 不在索引中”
【发布时间】:2021-07-22 18:48:43
【问题描述】:

我有以下代码:

将 2005 年至 2014 年间所有年份的数据框和仅 2015 年的数据框分开。 然后我得到所有气象站每天的最小最大值。 最后,我需要将 df2015 的最小值和最大值添加到 df 中,以便比较 2015 年的最小值和最大值是否打破了前 10 天的最小值或最大值。

df = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv'.format(binsize))
    df['Date'] =  pd.to_datetime(df['Date'], infer_datetime_format=True)
    df = df.sort_values(['Date', 'ID'], ascending=[True, False])
    df['day'] = df['Date'].dt.day
    df['month'] = df['Date'].dt.month
    df['year'] = df['Date'].dt.year
    df = df[df['year'] < 2015]
    df = df.groupby(['month','day'])['Data_Value'].agg(['min', 'max']) 
    print(df)
    
    df2015 = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv'.format(binsize))
    df2015['Date'] =  pd.to_datetime(df2015['Date'], infer_datetime_format=True)
    df2015 = df2015.sort_values(['Date', 'ID'], ascending=[True, False])
    df2015['day'] = df2015['Date'].dt.day
    df2015['month'] = df2015['Date'].dt.month
    df2015['year'] = df2015['Date'].dt.year
    df2015 = df2015[df2015['year'] == 2015]
    df2015 = df2015.groupby(['month','day'])['Data_Value'].agg(['min', 'max']) 
    df2015.rename(columns = {'min':'min2015'}, inplace = True)
    df2015.rename(columns = {'max':'max2015'}, inplace = True)
    print(df2015)
    
    df_2_cols_to_use = ['month', 'day', 'min2015', 'max2015']
    new_df = pd.merge(df, df2015[df_2_cols_to_use], on=['month', 'day'], how='inner')
    print(dffinal)
    

所以基本上我需要根据月和日列向 df 添加 min2015 和 max2015 列

每个数据框如下所示:

 min  max
month day          
1     1   -160  156
      2   -267  139
      3   -267  133
      4   -261  106






 min2015  max2015
month day                  
1     1       -133       11
      2       -122       39
      3        -67       39

但是我得到了这个错误

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-26-ccd285c88c17> in <module>()
     42 
     43 
---> 44 leaflet_plot_stations(400,'fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89')

<ipython-input-26-ccd285c88c17> in leaflet_plot_stations(binsize, hashid)
     28 
     29     df_2_cols_to_use = ['month', 'day', 'min2015', 'max2015']
---> 30     new_df = pd.merge(df, df2015[df_2_cols_to_use], on=['month', 'day'], how='inner')
     31     print(dffinal)
     32 

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2051         if isinstance(key, (Series, np.ndarray, Index, list)):
   2052             # either boolean or fancy integer index
-> 2053             return self._getitem_array(key)
   2054         elif isinstance(key, DataFrame):
   2055             return self._getitem_frame(key)

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_array(self, key)
   2095             return self.take(indexer, axis=0, convert=False)
   2096         else:
-> 2097             indexer = self.ix._convert_to_indexer(key, axis=1)
   2098             return self.take(indexer, axis=1, convert=True)
   2099 

/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
   1228                 mask = check == -1
   1229                 if mask.any():
-> 1230                     raise KeyError('%s not in index' % objarr[mask])
   1231 
   1232                 return _values_from_object(indexer)

KeyError: "['month' 'day'] not in index"

编辑:

  1. 我重置了索引
  2. 我打印了 df 和 df2015 列。

我的输出是:

Index(['month', 'day', 'min', 'max'], dtype='object')
Index(['month', 'day', 'min2015', 'max2015'], dtype='object')

我更新的代码:

 df = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv'.format(binsize))
    df['Date'] =  pd.to_datetime(df['Date'], infer_datetime_format=True)
    df = df.sort_values(['Date', 'ID'], ascending=[True, False])
    df['day'] = df['Date'].dt.day
    df['month'] = df['Date'].dt.month
    df['year'] = df['Date'].dt.year
    df = df[df['year'] < 2015]
    #df = df.groupby(df['Date'].dt.strftime('%m-%d'))['Data_Value'].agg(['min', 'max'])
    df = df.groupby(['month','day'])['Data_Value'].agg(['min', 'max']) 
    df=df.reset_index()
    print(df.columns)
    
    df2015 = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv'.format(binsize))
    df2015['Date'] =  pd.to_datetime(df2015['Date'], infer_datetime_format=True)
    df2015 = df2015.sort_values(['Date', 'ID'], ascending=[True, False])
    df2015['day'] = df2015['Date'].dt.day
    df2015['month'] = df2015['Date'].dt.month
    df2015['year'] = df2015['Date'].dt.year
    df2015 = df2015[df2015['year'] == 2015]
    df2015 = df2015.groupby(['month','day'])['Data_Value'].agg(['min', 'max']) 
    df2015.rename(columns = {'min':'min2015'}, inplace = True)
    df2015.rename(columns = {'max':'max2015'}, inplace = True)
    df2015=df2015.reset_index()
    print(df2015.columns)
    df_2_cols_to_use = ['min2015', 'max2015']   
    #Finally:
    new_df = pd.merge(df, df2015[df_2_cols_to_use], on=['month', 'day'], how='inner')

全栈错误

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
   2133             try:
-> 2134                 return self._engine.get_loc(key)
   2135             except KeyError:

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)()

KeyError: 'month'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-19-b9dfd4ccf2f2> in <module>()
     43 
     44 
---> 45 leaflet_plot_stations(400,'fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89')

<ipython-input-19-b9dfd4ccf2f2> in leaflet_plot_stations(binsize, hashid)
     31     df_2_cols_to_use = ['min2015', 'max2015']
     32     #Finally:
---> 33     new_df = pd.merge(df, df2015[df_2_cols_to_use], on=['month', 'day'], how='inner')
     34 
     35 

/opt/conda/lib/python3.6/site-packages/pandas/tools/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator)
     59                          right_on=right_on, left_index=left_index,
     60                          right_index=right_index, sort=sort, suffixes=suffixes,
---> 61                          copy=copy, indicator=indicator)
     62     return op.get_result()
     63 if __debug__:

/opt/conda/lib/python3.6/site-packages/pandas/tools/merge.py in __init__(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator)
    541         (self.left_join_keys,
    542          self.right_join_keys,
--> 543          self.join_names) = self._get_merge_keys()
    544 
    545     def get_result(self):

/opt/conda/lib/python3.6/site-packages/pandas/tools/merge.py in _get_merge_keys(self)
    808                     if not is_rkey(rk):
    809                         if rk is not None:
--> 810                             right_keys.append(right[rk]._values)
    811                         else:
    812                             # work-around for merge_asof(right_index=True)

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2057             return self._getitem_multilevel(key)
   2058         else:
-> 2059             return self._getitem_column(key)
   2060 
   2061     def _getitem_column(self, key):

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   2064         # get column
   2065         if self.columns.is_unique:
-> 2066             return self._get_item_cache(key)
   2067 
   2068         # duplicate columns & possible reduce dimensionality

/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1384         res = cache.get(item)
   1385         if res is None:
-> 1386             values = self._data.get(item)
   1387             res = self._box_item_values(item, values)
   1388             cache[item] = res

/opt/conda/lib/python3.6/site-packages/pandas/core/internals.py in get(self, item, fastpath)
   3541 
   3542             if not isnull(item):
-> 3543                 loc = self.items.get_loc(item)
   3544             else:
   3545                 indexer = np.arange(len(self.items))[isnull(self.items)]

/opt/conda/lib/python3.6/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
   2134                 return self._engine.get_loc(key)
   2135             except KeyError:
-> 2136                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2137 
   2138         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)()

KeyError: 'month'

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    你只需要使用reset_index()重置df2015的索引:

    df2015=df2015.reset_index()
    #Finally:
    new_df = pd.merge(df, df2015[df_2_cols_to_use], on=['month', 'day'], how='inner')
    

    同时分组链reset_index()方法:

    df2015 = df2015.groupby(['month','day'])['Data_Value'].agg(['min', 'max']).reset_index()
    #Finally:
    new_df = pd.merge(df, df2015[df_2_cols_to_use], on=['month', 'day'], how='inner')
    

    注意:您还可以重置两个数据框的索引(如果您在合并时遇到任何问题)

    说明:

    您收到 KeyError 是因为“月”和“日”是索引...它们不是数据框的列:

    这部分引起了问题:

    df2015[df_2_cols_to_use]
    

    更新:

    问题原因:

    您收到关键错误,因为您没有在 df_2_cols_to_use 列表中包含“月”和“日”,然后您在 df2015 中传递该列表,即 df2015[df_2_cols_to_use],因此 df2015 中的选定列是 ['min2015', 'max2015'],而您正在将它们合并到['month','day'],所以它们在 df2015 中不存在,这就是原因

    重置索引后使用:

    df_2_cols_to_use = ['month', 'day', 'min2015', 'max2015'] 
    #Finally:
    new_df = pd.merge(df, df2015[df_2_cols_to_use], on=['month', 'day'], how='inner')
    

    【讨论】:

    • 我在 df 和 df2015 都尝试了 resetindex,然后错误变成: Key error 'month',我也在同一行尝试了 inline reset index,但发生了同样的错误。
    • @LuisValencia 检查df.columnsdf2015.columns 的输出,看看两个输出中是否都存在“月”?
    • 我得到了这个:Index(['month', 'day', 'min', 'max'], dtype='object') Index(['month', 'day', ' min2015', 'max2015'], dtype='object')
    • @LuisValencia 你能发布错误的完整回溯吗?
    • @LuisValencia 请检查更新...您收到关键错误,因为您没有在df_2_cols_to_use 列表中包含“月”和“日”
    【解决方案2】:

    这不是 pd.merge 的问题。问题出在df2015[df_2_cols_to_use]。 像这种形式的 Pandas DataFrame 的数据选择自动选择轴。 'month', 'day' 只是索引,不是普通的列,所以 pd 认为它们是索引。

    所以,它会起作用的:

    df_2_cols_to_use = ['min2015', 'max2015']
    new_df = pd.merge(df, df2015[df_2_cols_to_use], on=['month', 'day'], how='inner')
    

    【讨论】:

    • 这不起作用,然后我收到此错误:KeyError: 'month'
    【解决方案3】:

    这是一个 Pandas 错误。 pd.merge 中存在一个错误,用于显示 Pandas 版本

    真正正确的错误信息在 df2015[df_2_cols_to_use]

    你应该在 groupby 操作之后放置以下内容

    df.columns = list(map(''.join, df.columns.values))
    

    df_2_cols_to_use = ['min2015', 'max2015']
    df2015[[df_2_cols_to_use]]
    

    【讨论】:

    • 然后我得到这个错误:TypeError: unhashable type: 'list'
    猜你喜欢
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 2020-02-03
    • 2018-12-18
    • 2017-11-04
    • 2021-08-23
    相关资源
    最近更新 更多