Get startedGet started for free

Heatmap of Travel Times By Commute Mode

In this exercise, you will create a heatmap of national data comparing commute times (in minutes) and travel modes. You begin with data_row, a list of values from the single data row of a JSON API response object. Lists of travel modes(5) and commute times (9) have been created, and are printed to the console. You must reshape the single data row into a list of lists, construct a DataFrame suitable to pass to sns.heatmap, and create the heatmap.

The data row contains data on the 5 travel modes in groups of 9 commute times. An iteration is a complete set of commute times.

pandas and seaborn are loaded using the usual aliases.

This exercise is part of the course

Analyzing US Census Data in Python

View Course

Exercise instructions

  • Set iter_len to the length of the times list
  • In the list comprehension, construct a range with a start of 0, stop of the length of the data_row, and step of iter_len
  • Construct a heatmap with the commuting DataFrame as the first parameter; annotate the heatmap with the commute count in thousands (use integer division to divide by 1000)

Hands-on interactive exercise

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

# Set iter_len to the number of commute times
iter_len = ____

# Break row into list of lists by travel mode
data = [data_row[i:i+iter_len] for i in range(____)]

# Create DataFrame, set data type to int
commuting = pd.DataFrame(data=data, index=modes, columns=times)
commuting = commuting.astype(int)

# Create heatmap of commuters by mode by income
sns.heatmap(____, annot=____, fmt = "d", cmap="YlGnBu")
plt.xticks(rotation = 50)
plt.yticks(rotation = 50)
plt.show()
Edit and Run Code