I have a data frame of latitudes and longitudes. The goal is to apply the censusgeocode
package to the entire data frame where our x
is our latitude column and our y
is our longitude column. After this, I am hoping to get a data frame with the geoid, state, county, tract, and block as columns.
Is it possible to do this so that I do not have to iterate over every single geolocation in a for loop? I could try the apply function, however, the apply function would not necessarily be able to be adaptable to this kind of problem. Below is my code for defining the dataframe and adding the solution to:
import pandas as pd
import censusgeocode as cg
data = pd.DataFrame({'latitude':[40.811765, 40.30019, 39.2464], 'longitude':[-81.528874, -81.83743, -83.60476]})
And what I have tried so far that takes too long to do this with thousands of locations:
geo_list = []
for f, g in zip(data['x'], data['y']):
try:
census = cg.coordinates(x=g, y=f)['2010 Census Blocks'][0]
data = [census['GEOID'], census['STATE'], census['COUNTY'], census['TRACT'], census['BLOCK'], f, g]
geo_list.append(data)
except:
pass