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.
Este exercício faz parte do curso
Analyzing US Census Data in Python
Instruções do exercício
- Build a list
col_namesof 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
astypemethod on each column to assign the correct data type.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# 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()