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,230 points
740 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,440 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, 2021 in Python by Gitika
• 65,910 points
1,689 views
0 votes
0 answers

how to merge two data frames based on particular column in pandas python?

I want to  merge two data frames: df1 company,standard tata,A1 cts,A2 dell,A3 df2 company,return tata,71 dell,78 cts,27 hcl,23 I ...READ MORE

May 26, 2022 in Python by Kichu
• 19,050 points
241 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,560 points
34,089 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
383 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,090 points
2,343 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
3,256 views
0 votes
1 answer
+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
146,558 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,440 points
496 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP