Python Pandas合并找不到现有列(KeyError)

时间:2017-08-10 22:06:45

标签: python merge keyerror

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

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']

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

Data = pd.merge(temp, MeanData, how='inner',
                on=['GradeCode','Class', 'Supplier term']
                )

我一直得到KeyError说"供应商术语"无效。但正如你所看到的,它都在df&s中。那么为什么会这样呢?!

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'

两个dfs的前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 = 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

0 个答案:

没有答案