Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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 indices 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:

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, how can I shift the index into a new column called "index"?

Thanks

1 Answer

0 votes
by (41.4k points)

You can use merge, which is an inner join by default:

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

join is left join by default:

df1.join(df2)

and concat is outer join by default:

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

Related questions

Browse Categories

...