2017-07-23 58 views
1

我有两个dataframes我试图合并:的Python /大熊猫 - KeyError异常合并dataframes

target: 

    version city_id   code 
id                    
4   2  4 5736201000175 
26  2  3 8290265000183 
27  3  3 9529184000156 
30  3  3 9263064000150 
34  2  3 9312770000144 
54  1  3 8407830000140 
55  1  3 5590100000139 



city: 

    federation_unit_id  name 
id           
3     8 SAO PAULO 
4     8 CAMPINAS 
7     8 BARUERI 
8     8 BEBEDOURO 
9     8  SANTOS 

我想将它们合并合并target的 “city_id” 与city的 “ID”,在这样,最终的数据帧是这样的:

target: 

    version city_id   code federation_unit_id  name 
id                    
4   2  4 5736201000175      8 CAMPINAS 
26  2  3 8290265000183      8 SAO PAULO 
27  3  3 9529184000156      8 SAO PAULO 
30  3  3 9263064000150      8 SAO PAULO 
34  2  3 9312770000144      8 SAO PAULO 
54  1  3 8407830000140      8 SAO PAULO 
55  1  3 5590100000139      8 SAO PAULO 

为了实现这个目标,我想用下面的代码:

target=target.merge(city, left_on='city_id', right_on='id') 

但是它一直让我以下KeyError异常:

Traceback (most recent call last): 
    File "/file.py", line 12, in <module> 
target=target.merge(city, left_on='index', right_on='city_id') 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 4437, in merge 
copy=copy, indicator=indicator) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tools/merge.py", line 38, in merge 
copy=copy, indicator=indicator) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tools/merge.py", line 210, in __init__ 
self.join_names) = self._get_merge_keys() 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tools/merge.py", line 434, in _get_merge_keys 
right_keys.append(right[rk]._values) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 1997, in __getitem__ 
return self._getitem_column(key) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 2004, in _getitem_column 
return self._get_item_cache(key) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/generic.py", line 1350, in _get_item_cache 
values = self._data.get(item) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/internals.py", line 3290, in get 
loc = self.items.get_loc(item) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/indexes/base.py", line 1947, in get_loc 
return self._engine.get_loc(self._maybe_cast_indexer(key)) 
    File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154) 
    File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018) 
    File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368) 
    File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322) 
KeyError: 'id' 

我无法找出什么我做错了:/ 能有人帮助?

回答

2

城市数据帧id似乎是一个指标,尝试设置right_index=True

target.merge(city, left_on='city_id', right_index=True) 

enter image description here

5

您可以使用join

​​

join本质上是以指数为导向的。但是,您可以在构成左侧的数据框中指定要加入的替代列。如果我们在target上调用join方法,那么我们想要指定'city_id'作为替代列。 city数据帧已经有适当的索引。