python(熊猫)通过索引合并两个dataframes。

时间:2022-05-25 22:55:52

Hi I have the following 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

how do I merge on the indexes to get:

如何在索引中合并得到:

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

I ask because it is my understanding that merge() i.e. df1.merge(df2) uses columns to do the matching.In fact doing this I get:

我之所以问,是因为我理解合并()即df1.merge(df2)使用列来进行匹配。事实上我这样做了:

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

Is it bad practice to merge on index? Is it impossible? If so Ho can I shift the index into a new column called "index"?

在索引中合并是不好的做法吗?这是不可能的吗?如果是这样,我可以将索引转换为一个名为“index”的新列吗?

Thanks

谢谢

2 个解决方案

#1


67  

Use merge, by default there is inner join:

使用合并,默认情况下是内部连接:

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

Or join, by default there is left join:

或者加入,默认情况下是左连接:

df1.join(df2)

Or concat, by default there is outer join:

或者concat,默认情况下是外部连接:

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

#2


13  

you can use concat([df1, df2, ...], axis=1) in order to concatenate two or more DFs aligned by indexes:

您可以使用concat([df1, df2,…[endnoteref: 1]为了将两个或两个以上的DFs串接在一起:

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

or merge for concatenating by custom fields / indexes:

或通过自定义字段/索引进行合并:

# 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)

or join for joining by index:

或加入索引:

 df1.join(df2)

#1


67  

Use merge, by default there is inner join:

使用合并,默认情况下是内部连接:

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

Or join, by default there is left join:

或者加入,默认情况下是左连接:

df1.join(df2)

Or concat, by default there is outer join:

或者concat,默认情况下是外部连接:

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

#2


13  

you can use concat([df1, df2, ...], axis=1) in order to concatenate two or more DFs aligned by indexes:

您可以使用concat([df1, df2,…[endnoteref: 1]为了将两个或两个以上的DFs串接在一起:

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

or merge for concatenating by custom fields / indexes:

或通过自定义字段/索引进行合并:

# 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)

or join for joining by index:

或加入索引:

 df1.join(df2)