1

I have a pandas dataframe that looks like this:

City Lat Long
Moscow 55.7 37.3
Paris 48.8 2.2
New York 40.7 -74.0
Moscow 55.6 37.4
Paris 48.9 2.3
New York 40.8 -74.1
Moscow 55.8 37.2
Paris 48.6 2.0
New York 40.9 -74.2

Let's say these lat/long values are completely inaccurate, so I want to replace them with actual coordinates of these cities.

I have another DataFrame (or a dictionary, if needed) that stores accurate coordinates for each City:

City Lat Long
Moscow 55.751244 37.395744
Paris 48.858093 2.294694
New York 40.712772 -74.006058

What's the easier way to update Lat / Long values for each City in the first table using values from the 2nd table?

FYI, the actual dataset I am working with includes ~ 2 million rows with approximately 500 unique values in the 'City' field.

1 Answer 1

1

Set the index of both dataframes to the same column ('City') and update df with df2, to override df1's values:

df1 = df1.set_index('City')

df2 = df2.set_index('City')

df1.update(df2)

df1

                Lat       Long
City
Moscow    55.751244  37.395744
Paris     48.858093   2.294694
New York  40.712772 -74.006058
Moscow    55.751244  37.395744
Paris     48.858093   2.294694
New York  40.712772 -74.006058
Moscow    55.751244  37.395744
Paris     48.858093   2.294694
New York  40.712772 -74.006058
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.