0

I have a main dataframe with county names. I have another data frame with county names and their latitude. I want to create a new latitude column in the main df for matching county names. The main df has some not matching names.

Main code:

df = 
            County
0           Maricopa
1       Hillsborough
2              Henry
3               Ogle
4           Mitchell
5           Melbourne

url='https://public.opendatasoft.com/explore/dataset/us-zip-code-latitude-and-longitude/download/?format=csv&timezone=America/New_York&lang=en&use_labels_for_header=true&csv_separator=%3B' 
uszip=pd.read_csv(url,sep=";")
df['Latitude'] = df['County'].map(dict(uszip[['City','Latitude']]))

df = 
            County Latitude
0         Maricopa      NaN
1     Hillsborough      NaN
2            Henry      NaN
3             Ogle      NaN
4         Mitchell      NaN
5         Melbourne     NaN
Mainland
  • 4,110
  • 3
  • 25
  • 56

1 Answers1

2

Are you looking to get all the county names in your main df and another Latitude column which has a matching country in your latitude df?

A Left join of your latitude df to your main df would give you the results you are looking for.

df_main = pd.merge(df_main, df_latitude[['country', 'latitude']], on='country', how='left')
Tukki
  • 90
  • 7
  • Both dfs different length and no index matching. So, here we need to write some condition. I have no idea how? – Mainland Jun 04 '21 at 02:05
  • Doesn't matter if it's different length, are country names different in both the dfs? Example : Australia in df1 and Austliar in df2? – Tukki Jun 04 '21 at 02:14