Get startedGet started for free

State-to-State Flows

In the video, you saw a heatmap of state-to-state migration flows, but it was cluttered. In this exercise, you will look only at flows within the Midwest.

A DataFrame state_to_state has been loaded, and the first few rows are displayed in the console. Remember from the video that the row labels indicate the state moved to, while the column names indicate the state moved from.

A list midwest_states has been defined with the names of the Midwestern states. (Print it to the console if you would like to see what states it includes.) The DataFrame also uses state names for the column names and index, so you will use midwest_states to extract the columns and rows you want to use for this heatmap.

pandas and seaborn are imported using the usual aliases.

This exercise is part of the course

Analyzing US Census Data in Python

View Course

Exercise instructions

  • Subset the date frame to return only those columns matching the list of state names, and only those rows whose indexes are in the list of state names.
  • Subsetting may have reordered the columns and rows. Check whether midwest.index is equal to midwest.columns.
  • Sort the DataFrame by row index (axis = 0) and column name (axis = 1). Use inplace = True in both cases.
  • Create a heatmap of midwest. Apply a yellow-green-blue color ramp with cmap="YlGnBu".

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Retain only rows and columns of Midwest states
midwest = state_to_state[____][state_to_state.index.isin(____)]

# Are rows and columns still in the same order?
print(____)

# Sort the rows (by index) and columns (by name)
midwest.sort_index(axis = ____, ____)
midwest.sort_index(axis = ____, ____)

# Create a heatmap of migration flows
____
plt.xticks(rotation=90)
plt.yticks(rotation=0)
plt.show()
Edit and Run Code