2017-01-23 87 views
21

我想加盟兩隻大熊貓數據採用兩列框:大熊貓:合併(加入)上的多個列中的兩個數據幀

new_df = pd.merge(A_df, B_df, how='left', left_on='[A_c1,c2]', right_on = '[B_c1,c2]') 

而且得到了以下錯誤:

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

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

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

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

KeyError: '[B_1, c2]' 

任何想法什麼應該是正確的方式來做到這一點?謝謝!

+9

'left_on'和'right_on'應該是一個字符串列表,而不是看起來像一個列表的字符串。 – root

回答

51

試試這個

new_df = pd.merge(A_df, B_df, how='left', left_on=['A_c1','c2'], right_on = ['B_c1','c2']) 

http://pandas.pydata.org/pandas-docs/version/0.19.1/generated/pandas.DataFrame.merge.html

left_on : label or list, or array-like Field names to join on in left DataFrame. Can be a vector or list of vectors of the length of the DataFrame to use a particular vector as the join key instead of columns

right_on : label or list, or array-like Field names to join on in right DataFrame or vector/list of vectors per left_on docs