How to merge two dataframes without getting additional rows?

139
August 12, 2021, at 11:50 PM

Basically, i have two dataframes, the first one looks like this:

df1

And the second one like this:

df2

I want to get the columns "lat" and "lnt" of the second one and add to the first one only if the name of the city matches on both dataframes. I tried using "pd.merge()", but it's creating new rows with duplicated values. If possible, i would like to put a NaN in the rows which didn't have any match at all, but i don't want to remove nor add rows to the original dataframe

Answer 1
merged_df = df1.merge(df2, how = 'inner', on = ['City'])
Answer 2

The Pandas merge function defaults to an inner join. Since you're looking to merge in the columns of df2 to df1, you should use a left join. This will give you all the rows of df1, and the matching values from df2.

df3 = df1.merge(df2, on = 'city', how = 'left')

Rent Charter Buses Company
READ ALSO
Clicking in an element on Puppeteer, using selectors

Clicking in an element on Puppeteer, using selectors

I'm trying to use puppeteer to go to a webpage and then click in a elementI'm using page

61
Correctly synchronizing/locking access to an ArrayList

Correctly synchronizing/locking access to an ArrayList

I am working on my first Spring-Boot application, after not touching Java in agesBelow is one of my classes

154
Webview google login not showing accounts on device

Webview google login not showing accounts on device

I have a progressive web app and I'm trying to create a simple app for it that has a WebView that is showing the PWA

165