If you're looking for the latitude and longitude of places in Singapore, you can make use of OneMap API. It is free for public use.
Below is an example how you can call the API:
import pandas as pd, numpy as np
import requests
import json
url = "https://www.onemap.gov.sg/api/common/elastic/search?searchVal=Jurong West Street 82 &returnGeom=Y&getAddrDetails=Y&pageNum=1"
response = requests.request("GET", url)
print(response.text)
![](https://static.wixstatic.com/media/1ea3da_c20c2e2336b345ffafe23591d21214f1~mv2.png/v1/fill/w_980,h_407,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/1ea3da_c20c2e2336b345ffafe23591d21214f1~mv2.png)
However for some locations, they might return more than one result as the road can be very long, for example, an expressway.
url = "https://www.onemap.gov.sg/api/common/elastic/search?searchVal=PIE towards Changi&returnGeom=Y&getAddrDetails=Y&pageNum=1"
![](https://static.wixstatic.com/media/1ea3da_3af3d3514d9c4f838882b4140ebc6318~mv2.png/v1/fill/w_980,h_398,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/1ea3da_3af3d3514d9c4f838882b4140ebc6318~mv2.png)
Let's say I have a dataframe that contains a list of locations that I would like to get the latitude and longitude values for. For simplicity, I will take the first pair returned within the results of the API call.
latlon_list = []
for i in range(0,2): #len(mydataset_df["location"])
location = mydataset_df["location"][i]
url = "https://www.onemap.gov.sg/api/common/elastic/search?searchVal={}&returnGeom=Y&getAddrDetails=Y&pageNum=1".format(mydataset_df["location"][i])
response = requests.request("GET", url)
k_json = json.loads(response.text)
latitude = k_json["results"][0]['LATITUDE']
longitude = k_json["results"][0]['LONGITUDE']
latlon_list.append((location, latitude, longitude))
latlon_df = pd.DataFrame(np.array(latlon_list))
latlon_df.columns = ['location','latitude','longitude']
The jupyter notebook containing the code is also up on Github here: https://github.com/hxchua/datadoubleconfirm/blob/master/notebooks/singapore_latlon.ipynb. We are importing a Dataiku dataset into a dataframe at the start and hence you will notice Dataiku related packages are involved. You can still disregard the Dataiku related packages if you are not using Dataiku. You can still do this regardless as long as you have a list of locations to iterate through.
Comments