I have a function that returns latitude and longitude information. I want to create columns for these 4 variables in a data frame.
Here is my code:
import geocoder
import pandas as pd
import geolib
from geolib import geohash
df = pd.read_csv('New_DP2.csv')
key = [redacted]
fields = ['NWLat', 'NWLong', 'SELat', 'SELong']
def getData(address, key):
g = geocoder.mapquest(address, key=key)
lat = g.lat
lng = g.lng
h = geolib.geohash.encode(lat, lng, 7)
hashes = geolib.geohash.neighbours(h)
NW = geohash.decode(hashes.nw)
SE = geohash.decode(hashes.ne)
nwlat = NW.lat
nwlon = NW.lon
selat = SE.lat
selon = SE.lon
I want to create four columns in a data frame that will make columns for 'nwlat','nwlon', 'selat', 'selon'.
Normally I would simply return nwlat and then create a lambda
df['NWLong'] = df.apply(lambda row: getData(row['a'], key), axis = 1)
Then I would do this for each case of the other 3 variables I want returned. But then I am running this a total of 4 times instead of just once.