How to merge two DataFrame in Pandas

0 votes

Hi Guys,

I have two DataFrame in Pandas. I want to merge these two DataFrame. How can I do this?

Jun 23, 2020 in Python by akhtar
• 38,210 points
195 views

1 answer to this question.

0 votes

Hi@akhtar,

You can use Pandas.merge() function to merge your DataFrame. I have attached one example below for your reference.

import pandas as pd

df1 = pd.DataFrame({"HPI":[80,90,70,60],"Int_Rate":[2,1,2,3], "IND_GDP":[50,45,45,67]}, index=[2001, 2002,2003,2004])
HPI Int_Rate IND_GDP
2001 80 2 50
2002 90 1 45
2003 70 2 45
2004 60 3 67

df2 = pd.DataFrame({"HPI":[80,90,70,60],"Int_Rate":[2,1,2,3],"IND_GDP":[50,45,45,67]}, index=[2005, 2006,2007,2008])

HPI Int_Rate IND_GDP
2005 80 2 50
2006 90 1 45
2007 70 2 45
2008 60 3 67
merged= pd.merge(df1,df2,on ="HPI")

HPI Int_Rate_x IND_GDP_x Int_Rate_y IND_GDP_y
0 80 2 50 2 50
1 90 1 45 1 45
2 70 2 45 2 45
3 60 3 67 3 67
answered Jun 23, 2020 by MD
• 95,300 points

Related Questions In Python

0 votes
1 answer

How to combine two columns of text in pandas dataframe?

If both columns are strings, you can ...READ MORE

answered Jan 5 in Python by Gitika
• 65,950 points
620 views
0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,520 points
30,195 views
0 votes
1 answer

How to merge two dictionaries in a single expression?

In Python 3.5 or greater: z = {**x, ...READ MORE

answered Mar 13, 2019 in Python by Trisha
159 views
0 votes
1 answer

How to find an element in Pandas Dataframe?

You can do it like this: Suppose df is ...READ MORE

answered Apr 8, 2019 in Python by Tina
27,836 views
0 votes
1 answer

Python: Issue with 'unexpected end of pattern'

I should start by stating that using ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 58,140 points
960 views
0 votes
1 answer

Python: Issue with 'unexpected end of pattern'

I should start by stating that using ...READ MORE

answered Sep 24, 2018 in Python by Priyaj
• 58,140 points
859 views
0 votes
1 answer

How to create Pandas series from numpy array?

Hi. Refer to the below command: import pandas ...READ MORE

answered Apr 1, 2019 in Python by Pavan
2,072 views
+1 vote
3 answers

How to change/update cell value in Python Pandas dataframe?

You can use the at() method to ...READ MORE

answered Apr 8, 2019 in Python by Kunal
130,013 views
0 votes
1 answer

How to set colume as index in Pandas DataFrame?

Hi@akhtar, You can use set_index function in pandas. ...READ MORE

answered Jun 23, 2020 in Python by MD
• 95,300 points
138 views