Unstack data frame after grouping to create heat matrix

Python tutorial to unstack the row categories into columns (long to wide table) to later create a heat matrix.

The following table represents a heat matrix that highlights the total amount of electricity, in terawatt-hours (TWh), produced by each source within the US48 power grid during 2023.

Heat matrix showcasing electricity generation by source in the US48 power grid for 2023.
F1. Heat matrix of electricity generation by source in 2023

To create the heat matrix from the raw dataset, which typically comes in a long format, we need to apply several steps that will be covered in this tutorial.

Before we begin, let’s understand the dataset we are working with.


The US power grid produces electricity from various sources, such as natural gas, solar, and petroleum, every hour. The amount of electricity produced by each source is recorded in each row of the dataset.

The data is provided by the US Energy Information Administration (EIA). Check out this tutorial to learn how to retrieve the data from the EIA API.

import pandas as pd
df = pd.read_csv('data.csv')
Base DataFrame showing the initial format of electricity generation data before processing.
F2. Initial DataFrame of electricity generation data


  1. How can we transform a DataFrame from long to wide format?
  2. Which function summarizes data into categories?
  3. How do we visualize data trends through a heat matrix?

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to datons.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.