LoslegenKostenlos loslegen

API to Visualization: Group Quarters

In this exercise, you will investigate where juvenile offenders are incarcerated. This exercise introduces the concept of "group quarters" populations, which includes college dorms, correctional facilities, nursing homes, military bases, etc.

You will visualize the percentage, by state, of incarcerated minor males in adult correctional facilities. The variables to request are:

  • PCT021005 - Male: Under 18 years: Institutionalized population: Correctional facilities for adults
  • PCT021015 - Male: Under 18 years: Institutionalized population: Juvenile facilities: Correctional facilities intended for juveniles

requests has been imported. The base_url for the API request has been defined.

pandas and seaborn have been imported using the usual aliases.

Diese Übung ist Teil des Kurses

Analyzing US Census Data in Python

Kurs anzeigen

Anleitung zur Übung

  • Complete the list get_vars with the two desired variables
  • Use astype to convert the columns in_adult and in_juvenile to integer
  • Calculate the percentage of minors in correctional facilities for adults: the numerator should be 100 times the number of minors in_adult correctional facilities; the denominator should be the sum of minors in_adult and in_juvenile correctional facilities
  • Sort states by pct_in_adult in descending order, using inplace = True

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Specify variables and execute API request
get_vars = ["NAME", ____]
predicates["get"] = ",".join(get_vars)
r = requests.get(base_url, params=predicates)

# Construct DataFrame
col_names = ["name", "in_adult", "in_juvenile", "state"]
states = pd.DataFrame(columns=col_names, data=r.json()[1:])
states[["in_adult", "in_juvenile"]] = states[["in_adult", "in_juvenile"]].____

# Calculate percentage of incarcerated male minors in adult facilities
states["pct_in_adult"] = ____
states.sort_values(by = ____, ascending = ____, inplace = ____)
sns.stripplot(x = "pct_in_adult", y = "name", data = states)
plt.show()
Code bearbeiten und ausführen