<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <a href="https
Step1:  Example
Step2:  Using get_places_in to Query Administrative Areas
The Pandas API defines a number of convenience functions for building Pandas dataframes with information in the datacommons graph. We will be using get_places_in which requires three arguments
Step3:  Let's see what states are in the USA
Step4:  Great! With the place dcids ready, we can build a dataframe with the population and median age for each state. To do that, we'll need to understand a little bit about queryng statistical data.
Querying Statistics from Data Commons
Data Commons has a large corpus of statistical data, which can be queried and joined with other statistics. For example, we can query the  median income of women living in Berkeley, California or the number of individuals who are insured in Maryland. 
Before we explore how to do this, we need to understand how Data Commons stores statistical data. In particular, there are two types of entities
Step5:  We view the data we've queried for.
Step6:  To get the name of places, we can use the get_property_values function
Step7:  Cleaning and Plotting the Data
Great! It looks like we have all the data we need. Before we finish, let's do some post-processing.
Step9:  and finally, let's visualize our results.
Step10:  We can also plot each administrative area granularity on the same plot to see how they relate.
<ASSISTANT_TASK:>
Python Code:
# Install datacommons_pandas
!pip install datacommons_pandas --upgrade --quiet
# Import Data Commons
import datacommons_pandas as dc
# Import other required libraries
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import pandas as pd
import json
Explanation: <a href="https://colab.research.google.com/github/datacommonsorg/api-python/blob/master/Analyzing_Census_Data_with_Data_Commons.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0
Notebook Version - 1.0.0
Analyzing Census Data with Data Commons
Every year, the American Community Survey (published by the US Census) reports thousands of variables about demographics, economics, housing, and more. This information, stored in Data Commons, is available to everyone for data science projects, education, and exploration. This tutorial introduces the Data Commons graph and two of its tools to help integrate its data into your data science projects: (1) the browser and (2) the Pandas API.
What is Data Commons?
Data Commons is an open knowledge repository that combines data from public datasets using mapped common entities. It contains statements about real world objects such as
Santa Clara County is contained in the State of California
The latitude of Berkeley, CA is 37.8703
The population of Maryland was 6,003,435 in 2018.
In the graph, entities like Santa Clara County are represented by nodes. Every node has a type corresponding to what the node represents. For example, California is a State. Relations between entities are represented by edges between these nodes. For example, the statement "Santa Clara County is contained in the State of California" is represented in the graph as two nodes: "Santa Clara County" and "California" with an edge labeled "containedInPlace" pointing from Santa Clara to California. Data Commons closely follows the Schema.org data model and leverages Schema.org schema to provide a common set of types and properties.
Data Commons Browser
The Data Commons browser provides a way to explore the data in a human-readable format. It is the best way to explore what is in Data Commons. Searching in the browser for an entity like Mountain View, takes you to a page about the entity, including properties like containedInPlace and timezone.
An important property for all entities is the dcid. The dcid (Data Commons identifier) is a unique identifier assigned to each entity in the knowledge graph. With this identifier, you will be able to search for and query information on the given entity in ways that we will discuss later. The dcid is listed at the top of the page next to "About: " and also in the list of properties.
Pandas API
The Pandas API provides functions for users to extract structured information from Data Commons programmatically and view them in different formats such as Python dicts and pandas DataFrames. DataFrames allow access to all the data processing, analytical and visualization tools provided by packages such as pandas, NumPy, SciPy, and Matplotlib.
Let's begin by loading the Data Commons Pandas API and the standard data science libraries:
End of explanation
# In the browser, we saw that the dcid for United States is country/USA
usa = 'country/USA'
Explanation: Example: Median Age vs Population by State, County, and City
For this exercise, we will be comparing the median ages and population count for US states, counties, and cities. First, let's lookup the dcid for the 'United States'.
End of explanation
# Get lists of states, counties, and cities within the United States, respectively.
states = dc.get_places_in([usa], 'State')[usa]
counties = dc.get_places_in([usa], 'County')[usa]
cities = dc.get_places_in([usa], 'City')[usa]
Explanation: Using get_places_in to Query Administrative Areas
The Pandas API defines a number of convenience functions for building Pandas dataframes with information in the datacommons graph. We will be using get_places_in which requires three arguments:
dcids - A list or pandas.Series of dcids identifying administrative areas that we wish to get containing places for.
place_type - The type of the administrative area that we wish to query for.
In Data Commons, the 'containedInPlace' property relates administrative areas to its containing administrative areas. Concretely, every US 'State' node has a directed edge to the USA 'Country' node where the name of this edge is 'containedInPlace'. The same also goes for 'County' to 'State' nodes and 'City' to 'County'.
When we provide a list of places to get_places_in, we get back a dict mapping each place to all the places contained inside it. In this case, we are only providing a single-element list of USA.
End of explanation
# Display the first 10 states
states[:10]
Explanation: Let's see what states are in the USA:
End of explanation
# Get StatVarObservations for states.
df_state = dc.build_multivariate_dataframe(states, ['Count_Person', 'Median_Age_Person'])
# Get StatVarObservations for counties.
df_county = dc.build_multivariate_dataframe(counties, ['Count_Person', 'Median_Age_Person'])
# Get StatVarObservations for cities.
df_city = dc.build_multivariate_dataframe(cities, ['Count_Person', 'Median_Age_Person'])
Explanation: Great! With the place dcids ready, we can build a dataframe with the population and median age for each state. To do that, we'll need to understand a little bit about queryng statistical data.
Querying Statistics from Data Commons
Data Commons has a large corpus of statistical data, which can be queried and joined with other statistics. For example, we can query the  median income of women living in Berkeley, California or the number of individuals who are insured in Maryland. 
Before we explore how to do this, we need to understand how Data Commons stores statistical data. In particular, there are two types of entities: StatisticalVariable and StatVarObservation.
A StatisticalVariable represents any type of statistical metric that can be measured at a place and time. Some examples include: median income, median income of females, number of high school graduates, unemployment rate, prevalence of diabetes, essentially anything you might call a metric, statistic, or measure. A StatVarObservation represents an actual measurement of a StatisticalVariable in a given place and time.
One example of a StatisticalVariable is the median age of people in San Antonio, Texas in 2014. The statistical metric, time, and place here are median age, 2014, and San Antonio respectively. A list of StatisticalVariables can be found here. To read more about StatisticalVariable and StatVarObservation, please see representing_statistics.md.
Data Commons defines APIs allowing us to fetch data over these two types. To begin with, we can use the build_multivariate_dataframe function to build a dataframe with the latest StatVarObservations of multiple StatisticalVariables for multiple places.
We are going to retrieve the total count and median age of populations (StatisticalVariables Count_Person and Median_Age_Person) for US states, counties, and cities.
Note - This query may take a minute!
End of explanation
# View the first 5 rows of the state table.
df_state.head(5)
Explanation: We view the data we've queried for.
End of explanation
def add_name_col(df):
  # Add a new column called name, where each value is the name for the place dcid in the index.
  df['name'] = df.index.map(dc.get_property_values(df.index, 'name'))
  
  # Keep just the first name, instead of a list of all names.
  df['name'] = df['name'].str[0]
add_name_col(df_state)
df_state.head()
Explanation: To get the name of places, we can use the get_property_values function:
End of explanation
# Filter for all cities that have at least one person
df_city = df_city[df_city['Count_Person'] >= 1]
Explanation: Cleaning and Plotting the Data
Great! It looks like we have all the data we need. Before we finish, let's do some post-processing.
End of explanation
def plot_data(title, pd_table):
   Generate a scatter plot comparing median age and population count. 
  plt.figure(figsize=(12, 8))
  plt.title(title)
  plt.xlabel('Median Age in Years')
  plt.ylabel('Population Count (log scale)')
  
  # Scatter plot the information
  ax = plt.gca()
  ax.set_yscale('log')
  ax.scatter(pd_table['Median_Age_Person'], pd_table['Count_Person'], alpha=0.7)
# Generate the plot for state data
plot_data('Median Age vs. Population Count for States', df_state)
# Generate the plot for county data
plot_data('Median Age vs. Population Count for Counties', df_county)
# Generate the plot for city data
plot_data('Median Age vs. Population Count for Cities', df_city)
Explanation: and finally, let's visualize our results.
End of explanation
def plot_all_data(state_table, county_table, city_table):
  plt.figure(figsize=(12, 8))
  plt.title('Median Age vs. Population Count')
  plt.xlabel('Median Age in Years')
  plt.ylabel('Population Count (log scale)')
  
  # Make things pretty
  state_color = "#ffa600"
  county_color = "#bc5090"
  city_color = "#003f5c"
  
  # Scatter plot the information
  ax = plt.gca()
  ax.set_yscale('log')
  ax.scatter(state_table['Median_Age_Person'], state_table['Count_Person'], color=state_color, alpha=0.75)
  ax.scatter(county_table['Median_Age_Person'], county_table['Count_Person'], color=county_color, alpha=0.5)
  ax.scatter(city_table['Median_Age_Person'], city_table['Count_Person'], color=city_color, alpha=0.4)
  
  # Create the legend
  state_patch = mpatches.Patch(color=state_color, label='States')
  county_patch = mpatches.Patch(color=county_color, label='Counties')
  city_patch = mpatches.Patch(color=city_color, label='Cities')
  plt.legend(handles=[state_patch, county_patch, city_patch])
# Plot all the data together.
plot_all_data(df_state, df_county, df_city)
Explanation: We can also plot each administrative area granularity on the same plot to see how they relate.
End of explanation
<END_TASK>