Get startedGet started for free

Pivot temperature by city and year

It's interesting to see how temperatures for each city change over time—looking at every month results in a big table, which can be tricky to reason about. Instead, let's look at how temperatures change by year.

You can access the components of a date (year, month and day) using code of the form dataframe["column"].dt.component. For example, the month component is dataframe["column"].dt.month, and the year component is dataframe["column"].dt.year.

Once you have the year column, you can create a pivot table with the data aggregated by city and year, which you'll explore in the coming exercises.

pandas is loaded as pd. temperatures is available.

This exercise is part of the course

Data Manipulation with pandas

View Course

Exercise instructions

  • Add a year column to temperatures, from the year component of the date column.
  • Make a pivot table of the avg_temp_c column, with country and city as rows, and year as columns. Assign to temp_by_country_city_vs_year, and look at the result.

Hands-on interactive exercise

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

# Add a year column to temperatures
____

# Pivot avg_temp_c by country and city vs year
temp_by_country_city_vs_year = ____

# See the result
print(temp_by_country_city_vs_year)
Edit and Run Code