2016-11-07 104 views
23

嗨,我有以下dataframes:蟒蛇(熊貓)合併兩個dataframes通過指數

> df1 
    id begin conditional confidence discoveryTechnique 
0 278 56  false  0.0     1 
1 421 18  false  0.0     1 

> df2 
    concept 
0 A 
1 B 

我怎麼合併的索引來獲得:

id begin conditional confidence discoveryTechnique concept 
0 278 56  false  0.0     1 A 
1 421 18  false  0.0     1 B 

我問,因爲這是我的理解這merge()df1.merge(df2)使用列做matching.In其實這樣做,我得到:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 4618, in merge 
    copy=copy, indicator=indicator) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.py", line 58, in merge 
    copy=copy, indicator=indicator) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.py", line 491, in __init__ 
    self._validate_specification() 
    File "/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.py", line 812, in _validate_specification 
    raise MergeError('No common columns to perform merge on') 
pandas.tools.merge.MergeError: No common columns to perform merge on 

在索引上合併是不好的做法嗎?這是不可能的嗎?如果是這樣的話,我可以將索引轉換成一個名爲「索引」的新列嗎?

感謝

+1

試試這個:'df1.join(df2)' – MaxU

回答

51

使用merge,默認情況下有內連接:

pd.merge(df1, df2, left_index=True, right_index=True) 

或者join,默認情況下有左連接:

df1.join(df2) 

或者concat,默認情況下有外連接:

pd.concat([df1, df2], axis=1) 
+1

謝謝!我用'合併',對我來說很完美。 – brucezepplin

+1

不錯。對於其他讀者來說,如果它不工作,看看你是否需要'.transpose()'你的一個dfs同步索引 - 這是我的問題 – Jona

13

可以使用concat([df1, df2, ...], axis=1)以串聯的指標排列的兩個或兩個以上的DF:

pd.concat([df1, df2, df3, ...], axis=1) 

merge通過自定義字段/指標串聯:

# join by _common_ columns: `col1`, `col3` 
pd.merge(df1, df2, on=['col1','col3']) 

# join by: `df1.col1 == df2.index` 
pd.merge(df1, df2, left_on='col1' right_index=True) 

join通過指數加盟:

df1.join(df2) 
+0

非常感謝 - 合併的列規範是有用的。 – brucezepplin