The API Response and Pandas
In this exercise you will load data from an API response object into a pandas
DataFrame. You will assign user-friendly column names and convert the values from strings to appropriate data types.
After creating the DataFrame, run the sample code to create a scatterplot to visualize the relationship between average family size and median age in the United States.
requests
and pandas
(as pd
) have already been imported. A response object r
is loaded.
This exercise is part of the course
Analyzing US Census Data in Python
Exercise instructions
- Build a list
col_names
of 4 new column names:name
,median_age
,avg_family_size
, andstate
- Use the DataFrame constructor to create the DataFrame
states
. The data parameter should be set tor.json()
, but use slicing to skip the first item, which contains the old column names - Use the
astype
method on each column to assign the correct data type.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import seaborn
import seaborn as sns
sns.set()
# Construct the DataFrame
col_names = ____
states = pd.DataFrame(columns = col_names, data = ____)
# Convert each column with numeric data to an appropriate type
states["median_age"] = states["median_age"].____
states["avg_family_size"] = ____
# Scatterplot with regression line
sns.lmplot(x = "avg_family_size", y = "median_age", data = states)
plt.show()