Python Pandas merge找不到现有列(keyrerror)

2024-10-02 08:21:39 发布

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

我合并了两个数据帧,每个数据帧中的列名称是:

print('\ntemp data headers are: ', temp.columns.values)
print('\nmean mapped data columns are: ', MeanMapped.columns.values)

>>>
temp data headers are:  ['Class'
 'Regulation mins' 'GradeCode' 'Supplier term'
 'Regulation max']

mean mapped data columns are:  ['Grade' 'Class'
 'Regulation mean' 'GradeCode' 'Supplier term']

但是,当我尝试使用以下代码合并两个数据帧时:

^{pr2}$

我一直收到关键错误,说“供应商条款”是无效的。但正如你所见,这两个测向仪都有。那为什么会发生这种情况?!在

Traceback (most recent call last):
  File "C:\Users\yisli\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\indexes\base.py", line 2134, in get_loc
    return self._engine.get_loc(key)
  File "pandas\index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)
  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)
  File "pandas\src\hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)
  File "pandas\src\hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)
KeyError: 'Supplier term'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/yisli/Documents/landlordlady/8-10 main.py", line 76, in <module>
    on=['GradeCode','Class', 'Supplier term']
  File "C:\Users\yisli\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\tools\merge.py", line 61, in merge
    copy=copy, indicator=indicator)
  File "C:\Users\yisli\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\tools\merge.py", line 543, in __init__
    self.join_names) = self._get_merge_keys()
  File "C:\Users\yisli\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\tools\merge.py", line 810, in _get_merge_keys
    right_keys.append(right[rk]._values)
  File "C:\Users\yisli\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\frame.py", line 2059, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\yisli\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\frame.py", line 2066, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\yisli\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\generic.py", line 1386, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\yisli\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\internals.py", line 3543, in get
    loc = self.items.get_loc(item)
  File "C:\Users\yisli\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\indexes\base.py", line 2136, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)
  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)
  File "pandas\src\hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)
  File "pandas\src\hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)
KeyError: 'Supplier term'

两个df的前5行是: 温度数据:

temp = temp[['Class', 'Regulation mins', 'GradeCode', 'Supplier term', 'Regulation max']]
print(temp)
     Class              Regulation mins       GradeCode          Supplier term  \
0  Aluminum  StrainHardeningExponentLMin  6013-T4-S-ST-1         N-Bar   
1  Aluminum  StrainHardeningExponentLMin  6013-T4-S-ST-1         N-Bar   
2  Aluminum  StrainHardeningExponentLMin  6013-T4-S-ST-1         N-Bar   
3  Aluminum  StrainHardeningExponentLMin  6013-T4-S-ST-1         N-Bar   
4  Aluminum  StrainHardeningExponentLMin  6022-T4-S-IH-1         N-Bar   

                Regulation max  
0  StrainHardeningExponentLMax  
1  StrainHardeningExponentLMax  
2  StrainHardeningExponentLMax  
3  StrainHardeningExponentLMax  
4  StrainHardeningExponentLMax 

以及平均映射子集:

    MeanMapped = MeanMapped[['Class', 'Regulation mean', 'GradeCode', 'Supplier term']]
 print(MeanMapped.head())
          Class          Regulation mean       GradeCode Supplier term
    0  Aluminum  PlasticStrainRatioLMean  6013-T4-S-ST-1         R-bar
    1  Aluminum  PlasticStrainRatioLMean  6022-T4-S-IH-1         R-bar
    2  Aluminum  PlasticStrainRatioLMean  6022-T4-S-ST-1         R-bar
    3  Aluminum  PlasticStrainRatioLMean  6056-T4-S-ST-1         R-bar
    4  Aluminum  PlasticStrainRatioLMean  6016-T4-S-IH-1         R-bar

Tags: inpypandasgetindexlineusersloc

热门问题