sb32134
sb32134

Reputation: 430

how do merge two dataframe in pandas

I want to merge two dataframe based first column. Here is the code with suggestion from other:

import pandas as pd

movie_genres =[[u'Drama', u'Romance', u'Sci-Fi'],
 [u'Biography', u'Drama', u'History', u'War'],
 [u'Animation', u'Adventure', u'Family', u'Fantasy'],
 [u'Biography', u'Drama', u'History', u'War']]

movie_id = [u'0338013',
 u'0363163',
 u'0347149',
 u'0395169']

movie_year = [u'(2004)',
 u'(2004)',
 u'(2004)',
 u'(2004)']


df1 = pd.DataFrame(pd.Series(movie_genres, movie_id))
df2 = pd.DataFrame(pd.Series(movie_year, movie_id))


merge(df1, df2, how='left', on=None, left_on=None, right_on=None,
      left_index=False, right_index=False, sort=True,
      suffixes=('_x', '_y'), copy=True)

Here is the error i'm getting with above code:

/home/tets/code/viz/testing-dataframe.py in <module>()
     23 merge(df1, df2, how='left', on=None, left_on=None, right_on=None,
     24       left_index=False, right_index=False, sort=True,
---> 25       suffixes=('_x', '_y'), copy=True)

How to merge df1 and df2 having first column as index

Upvotes: 0

Views: 178

Answers (1)

ComputerFellow
ComputerFellow

Reputation: 12108

From the documentation on how to merge, it looks straightforward:

merge(left, right, how='left', on=None, left_on=None, right_on=None,
      left_index=False, right_index=False, sort=True,
      suffixes=('_x', '_y'), copy=True)

where leftis your df1, right is your df2 and on is your first column.

Upvotes: 1

Related Questions