Part 4 - What to enrich with? (What are Data Collections and Analysis Variables?)¶
Data Collections and GeoEnrichment coverage¶
As described earlier, a data collection is a preassembled list of attributes that will be used to enrich the input features. Collection attributes can describe various types of information, such as demographic characteristics and geographic context of the locations or areas submitted as input features.
Some data collections (such as default) can be used in all supported countries. Other data collections may only be available in one or a collection of countries. Data Browser can be used to examine the entire global listing of variables, and associated datasets for each country.
List Countries with GeoEnrichment Data¶
The get_countries()
method can be used to query the countries for which GeoEnrichment data is available, and it returns a list of Country
objects with which you can further query for properties. This list can also be viewed here.
# Import Libraries
from arcgis.gis import GIS
from arcgis.geoenrichment import Country, enrich
# Create a GIS Connection
gis = GIS(profile='your_online_profile')
countries = get_countries()
print("Number of countries for which GeoEnrichment data is available: " + str(len(countries)))
#print a few countries for a sample
countries[0:10]
Data Collections for U.S.¶
The data_collections
property of a Country
object lists its available data collections and analysis variables under each data collection as a Pandas dataframe.
In order to discover the data collections for a particular country, you may first access the reference variable to it using the country.get()
method, and then fetch the data collections from country.data_collections
property. Once we know the data collection we would like to use, we can look at analysisVariable
s available in that data collection.
# Get US as a country
usa = Country.get('US')
type(usa)
usa_df = usa.data_collections
# print a few rows of the DataFrame
usa_df.head()
usa_df.shape
Unique Data Collections for U.S.¶
Each data collection and analysis variable has a unique ID. When calling the enrich()
method (explained earlier in this guide) these analysis variables can be passed in the data_collections
and analysis_variables
parameters.
As an example, here we see a subset of the data collections for US showing 2 different data collections and multiple analysis variables for each collection.
usa_df.iloc[500:600,:]
The table above shows 2 different data collections (1yearincrements and 5yearincrements). Since these are Age
data collections, the analysisVariable
s for these collections are similar. vintage
shows the year that the demographic data represents. For example, a vintage of 2020 means that the data represents the year 2020.
Let's get a list of unique data collections that are available for U.S.
usa_df.index.nunique()
United States has 150 unique data collections. Here are the first 10 data collections.
list(usa_df.index.unique())[:10]
Looking at fieldCategory
is a great way to clearly understand what the data collection is about. fieldCategory
combines vintage, datacollectionID columns along with the year and data collection. However, to query a data collection its unique ID (dataCollectionID
) must be used.
Let's look at the fieldCategory
column for a few data collections in US.
usa_df.fieldCategory.unique()[:10]
Data Collections by Socio-demographic Factors¶
You can filter the data_collections
to get collections for a specific factor using Pandas expressions. Let's loook at data collections for different socio-demographic factors
such as Age, Population, Income
.
Data Collections for Age
Age_Collections = usa_df['fieldCategory'].str.contains('Age', na=False)
usa_df[Age_Collections].fieldCategory.unique()
Data Collections for Population
Pop_Collections = usa_df['fieldCategory'].str.contains('Population', na=False)
usa_df[Pop_Collections].fieldCategory.unique()
Data Collections for Income
Income_Collections = usa_df['fieldCategory'].str.contains('Income', na=False)
Income_Collections.index.unique()
As mentioned earlier, using a data_collection
's unique ID (dataCollectionID
) is the best way to further query a data collection. Let's look at the dataCollectionID
for various Income data collections.
usa_df[Income_Collections].index.unique()
Analysis variables for Data Collections¶
Once we know the data collection we would like to use, we can look at all the unique variables available in that data collection using its unique ID. Let's discover analysisVariable
s for some of the data collections.
Analysis variables for Age
data collection
usa_df.loc['Age']['analysisVariable'].unique()
Analysis variables are typically represented as dataCollectionID.<analysis variable name>
as seen above.
Analysis variables for Age_by_Sex_by_Race_Profile_rep
data collection
usa_df.loc['Age_by_Sex_by_Race_Profile_rep']['analysisVariable'].unique()
Analysis variables for DaytimePopulation
data collection
usa_df.loc['DaytimePopulation']['analysisVariable'].unique()
Data Collections for Another Country¶
Let's look at data collections for New Zealand. Data Browser can be used to examine the entire global listing of variables, and associated datasets for New Zealand.
In order to discover the data collections for a particular country, you may first access the reference variable to it using the country.get()
method, and then fetch the data collections from country.data_collections
property. Once we know the data collection we would like to use, we can look at analysisVariable
s available in that data collection.
# Get US as a country
nz = Country.get('New Zealand')
type(nz)
nz_df = nz.data_collections
# print a few rows of the DataFrame
nz_df.head()
nz_df.shape
Unique Data Collections for New Zealand¶
Let's get a list of unique data collections that are available for New Zealand.
nz_df.index.unique()
New Zealand has 12 unique data collections.
We can look at the fieldCategory
column to understand each category better.
nz_df.fieldCategory.unique()
Looking at fieldCategory
is a great way to clearly understand what the data collection is about. However, to query a data collection its unique ID (dataCollectionID
) must be used.
Data Collections for Socio-demographic Factors¶
New Zealand has fewer data_collections
compared to U.S. Let's look at data collections for Key Facts, Education and Spending.
Data Collection for Key Facts
nz_df.loc['KeyGlobalFacts']
Data Collection for Education
nz_df.loc['EducationalAttainment']
Data Collection for Spending
nz_df.loc['Spending']
Analysis variables for Data Collections¶
Once we know the data collection we would like to use, we can look at all the unique variables available in that data collection using its unique ID. Let's discover analysisVariable
s for some of the data collections we looked at earlier.
Analysis variables for KeyGlobalFacts
data collection
nz_df.loc['KeyGlobalFacts']['analysisVariable'].unique()
Analysis variables for EducationalAttainment
data collection
nz_df.loc['EducationalAttainment']['analysisVariable'].unique()
Analysis variables for Spending
data collection
nz_df.loc['Spending']['analysisVariable'].unique()
Perform Enrichment using Data Collections and Analysis Variables¶
Data Collections can be used to enrich various study areas. data_collection
s and analysis_variable
s can be passed in the enrich()
method. Details about enriching study areas can be found in Enriching Study Areas section.
Let's look at a few similar examples of GeoEnrichment here.
Enrich using Data Collections¶
Enrich with Age
data collection
Here we see an address being enriched by data from Age
data collection.
# Enriching single address as single line imput
age_coll = enrich(study_areas=["380 New York St Redlands CA 92373"],
data_collections=['Age'])
age_coll
age_coll.columns
When a data collection is specified without specific analysis variables, all variables under the data collection are used for enrichment as can be seen above.
Enrich with Health
data collection
Here we see a zip code being enriched by data from Health data collection.
redlands = usa.subgeographies.states['California'].zip5['92373']
redlands_df = enrich(study_areas=[redlands], data_collections=['Health'] )
redlands_df
redlands_df.columns
Enrich using Analysis Variables¶
Data can be enriched by specifying specific analysis variables of a data collection with which we want to enrich our data. In this example, we will look at analysis_variables
for Age data_collection
and then use specific analysis variables to enrich()
a study area.
# Unique analysis variables for Age data collection
usa = Country.get('US')
usa.data_collections.loc['Age']['analysisVariable'].unique()
Now, we will enrich our study area with Age.FEM45, Age.FEM55, Age.FEM65
variables
enrich(study_areas=["380 New York St Redlands CA 92373"],
analysis_variables=["Age.FEM45","Age.FEM55","Age.FEM65"])
Enriching Spatially Enabled Dataframes¶
One of the most common use case for GeoEnrichment is enriching existing data in feature layers. As a user, you may need to analyze and enrich your data that already exists in feature layers. Spatially Enabled DataFrame (SeDF) helps us bring the data from layer into a dataframe which can then be GeoEnriched.
Let's look at an example using an existing layer of Covid-19 dataset. This feature layer includes latest Covid-19 Cases, Recovered and Deaths data for U.S. at the county level.
# Get the layer
gis = GIS(set_active=False)
covid_item = gis.content.get('628578697fb24d8ea4c32fa0c5ae1843')
print(covid_item)
covid_layer = covid_item.layers[0]
covid_layer
We can query the layer as a dataframe and then use the dataframe for enrichment.
covid_df = covid_layer.query(as_df=True)
covid_df.shape
covid_df.head()
To showcase GeoEnrichment, we will create a subset of the original data and then enrich()
the subset.
# Create subset
test_df = covid_df.iloc[:100].copy()
test_df.shape
# Check geometry
test_df.spatial.geometry_type
A dataframe can be passed as a value to study_areas
parameter of the enrich()
method. Here we are enriching our dataframe with specific variables from Age
data collection.
# Enrich dataframe
new_df = enrich(study_areas=test_df,
analysis_variables=["Age.FEM45","Age.FEM55","Age.FEM65"])
new_df.head()
new_df.drop(['OBJECTID_0', 'ID','Last_Update'], axis=1, inplace=True)
# Check shape
new_df.shape
We can see that enrichment resulted in 91 records and 31 columns. There are some areas in our dataframe for which enrichment information is not available. Hence, we have 91 records instead of 100. Geoenrichment adds some additional columns along with the analysis variables we enriched for and so we see 31 columns however we are dropping duplicates and unnecessary columns to bring the count down to 28 columns.
Visualize on a Map¶
Let's visualize the enriched dataframe on a map. We will use FEM65
column to classify our data for plotting on the map.
covid_map = gis.map('USA', 4)
covid_map
# Plot on a map
covid_map.remove_layers()
new_df.spatial.plot(map_widget=covid_map,
renderer_type='c', # for class breaks renderer,
method='esriClassifyNaturalBreaks', # classification algorithm,
class_count=5, # choose the number of classes,
col='FEM65', # numeric column to classify,
cmap='viridis', # color map to pick colors from for each class,
alpha=0.7)
Conclusion¶
In this part of the arcgis.geoenrichment
module guide series, you saw how data_collections
property of a Country
object lists its available data_collection
s and analysis_variable
s. You explored different data collections, their analysis variables and then enriched study areas using the same. Towards the end, you experienced how spatially enabled dataframes can be enriched.
In the subsequent pages, you will learn about Generating Reports and Standard Geography Queries.
Feedback on this topic?