0

I have a large dataset with latitudes and longitudes and i want to map city and state in front of them. Approach which i was using is this:

import pandas as pd import reverse_geocoder as rg import pprint df = pd.read_csv("D:\data.csv") 
def reverseGeocode(coordinates): result = rg.search(coordinates) # result is a list containing ordered dictionary. pprint.pprint(result) # Driver function if __name__=="__main__": # Coordinates tuple.Can contain more than one pair. for i in range(2): coordinates =(df['latitude'][i],df['longitude'][i]) reverseGeocode(coordinates) 

Output:

[OrderedDict([('lat', '13.322'), ('lon', '75.774'), ('name', 'Chikmagalur'), ('admin1', 'Karnataka'), ('admin2', 'Chikmagalur'), ('cc', 'IN')])] [OrderedDict([('lat', '18.083'), ('lon', '73.416'), ('name', 'Mahad'), ('admin1', 'Maharashtra'), ('admin2', 'Raigarh'), ('cc', 'IN')])] 

What i want to do is -

 id latitude longitude name admin2 admin1 0 23 13.28637 75.78518 1 29 17.90387 73.43351 2 34 15.72967 74.49182 3 48 20.83830 73.26416 4 54 21.93931 75.13398 5 71 20.92673 75.32402 6 78 19.26049 73.38982 7 108 17.90468 73.43486 8 109 13.28637 75.78518 9 113 15.72934 74.49189 10 126 20.83830 73.26417 11 131 21.93930 75.13399 12 146 20.92672 75.32402 13 152 19.26049 73.38982 14 171 17.90657 73.43382 

map name admin1 and admin2 in my dataframe(df) in front of ["latitude","longitude"]

4
  • Hi @mjspier. Need some help with the above mentioned problem. Commented Nov 29, 2019 at 6:31
  • Please include all relevant code and data. We should be able to copy/paste a few things and have your programming running in less than a minute. If df is a DataFrame, what little code you shared is unidiomatic. Commented Nov 29, 2019 at 7:05
  • Hi @AlexanderCécile. I have lat, long multiple times and need to map a city in front of them. I have added some more rows for your reference. Hope it helps. Commented Nov 29, 2019 at 9:25
  • What's the format of the data in "D:\data.csv"? Also, I believe we're missing part of your code, right? Commented Dec 1, 2019 at 2:56

2 Answers 2

1

Although others solution may be valid, but you can find more elegant one:

import pandas as pd import reverse_geocoder as rg import pprint df = pd.read_csv("data.csv") def reverseGeocode(coordinates): result = rg.search(coordinates) return (result) if __name__=="__main__": # Coordinates tuple.Can contain more than one pair. coordinates =list(zip(df['latitude'],df['longitude'])) # generates pair of (lat,long) data = reverseGeocode(coordinates) df['name'] = [i['name'] for i in data] df['admin1'] = [i['admin1'] for i in data] df['admin2'] = [i['admin2'] for i in data] df.to_csv("data_appended.csv") # write to csv # result will be saved to data_appended.csv 

Output

Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to add Country name in there also?
0

Here's how:

if __name__=="__main__": df = pd.Dataframe(columns = ['latitude','longitude']) # Coordinates tuple.Can contain more than one pair. for i in range(2): coordinates =(df['latitude'][i],df['longitude'][i]) res = reverseGeocode(coordinates) lat = res[0]['lat'] long = res[0]['lon'] df = df.append([{ 'latitude': lat, 'longitude': long}]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.