如何在pandas中合并两个表,KeyError:' timestamp,city'

时间:2015-10-11 15:57:15

标签: python pandas

我无法加入2个数据框:

df = pandas.merge(df_a,df_b, on=['timestamp,city'], how='right')

cola             city  impression                 timestamp
0  San Francisco           6  2015-10-11T08:00:00.000Z
1  San Francisco          14  2015-10-11T09:00:00.000Z
2           None           2  2015-10-11T10:00:00.000Z
3  San Francisco          13  2015-10-11T10:00:00.000Z
4    Makati City           6  2015-10-11T11:00:00.000Z
5  San Francisco           5  2015-10-11T11:00:00.000Z
6    Makati City           3  2015-10-11T12:00:00.000Z
7    Makati City           3  2015-10-11T13:00:00.000Z

colb           city  click                 timestamp
0  Makati City      6  2015-10-11T12:00:00.000Z
1  Makati City      3  2015-10-11T13:00:00.000Z




 return self._engine.get_loc(_values_from_object(key))
  File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:3979)
  File "pandas/index.pyx", line 157, in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)
  File "pandas/hashtable.pyx", line 668, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12265)
  File "pandas/hashtable.pyx", line 676, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12216)
KeyError: 'timestamp,city'

1 个答案:

答案 0 :(得分:1)

您没有正确分隔列,on参数应该是这样的:

on = ['timestamp', 'city'] 

执行此操作的方式导致它搜索名为timestamp,city的列,由于它显然不存在,因此会生成KeyError