Pivot—The pivot() method

Learn how to apply various methods to pivot a DataFrame.

The pivot() method

While the long data format contains plenty of information stored in a simple structure, it’s challenging to summarize and analyze the many repeated rows of data directly.

We can use the pivot() method to convert data from a long to wide format. Suppose we have a long format dataset containing the yearly population (from 2010 to 2018) for the world’s top ten most populated countries, as displayed by the code below:

Press + to interact
# Show first 25 rows of population data for top 10 most populated countries
print(df.head(25))

The pivot() method requires us to specify the column names for three parameters:

  • index: Column will be used as the index (aka the “subject” column) and unique values will appear as rows.

  • columns: Column will be used as new column labels in the pivoted DataFrame and unique values will appear as columns.

  • values: Column will be used to populate the cells in the pivoted DataFrame.

Press + to interact
# Pivot the original long format DataFrame
df_pivot = df.pivot(index='Country', columns='Year', values='Value')
print(df_pivot)

The output above shows that pivot() has reshaped our original DataFrame into a wide format. Each row now represents one country, with multiple columns that contain the country’s population values over the years. The benefit of the wide format is evident here, because we can now easily compare the populations of different countries over time. ...

Get hands-on with 1400+ tech skills courses.