IamWarmduscher
IamWarmduscher

Reputation: 955

How do I merge two data frames using either concat or merge?

I'm trying to merge the two data frames that look like this: https://i.imgur.com/ZCPzx7V.png

What would I write to merge the two?

I've been through this but I am still getting errors:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

https://www.datacamp.com/community/tutorials/joining-dataframes-pandas

Upvotes: 0

Views: 44

Answers (2)

Suraj
Suraj

Reputation: 2477

To merge 2 dataframes, we need to have a column based on which the 2 dataframes can be combined.

df1 : letter | a_count and df2 : alpha | name These dataframes are combined based on the columns letter and alpha in df1 and df2 respectively.

For this, first we rename the column alpha as letter in df2 and then merge these 2 dataframes based on their common column letter

df2 = df2.rename(columns = {'alpha': 'letter'})
pd.merge(df1,df2, on='letter')

Upvotes: 0

Sajan
Sajan

Reputation: 1267

You could try this ( assuming that the first dataframe is df1 and the second one df2 ) -

pd.merge(df1, df2, left_on='letter', right_on='alpha')

Upvotes: 1

Related Questions