Skip to main content
Bumped by Community user
added 9 characters in body
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

I have a data frame of latitudes and longitudes. The goal is to apply the censusgeocodecensusgeocode package to the entire data frame where our xx is our latitude column and our yy 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

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

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
Source Link

Applying the censusgeocode Package to an Entire Dataframe of Geocoded Data

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