pivot() Method: Pivot DataFrame Without Aggregation Operation¶
Date published: 2018-11-24
Category: Data Analysis
Subcategory: Data Wrangling
Tags: pivot, python, pandas
In pandas, we can pivot our DataFrame without applying an aggregate operation. This pivot is helpful to see our data in a different way - often turning a format with many rows that would require scrolling into a new format with fewer rows but perhaps more columns.
Import Module¶
import pandas as pd
Example: Pivot Tesla Car Acceleration Details¶
Here is fictional acceleration tests for three popular Tesla car models. In order to verify acceleration of the cars, I figured a third-party may make three runs to test the three models alongside one another.
s = 'Tesla Model S P100D'
x = 'Tesla Model X P100D'
three = 'Tesla Model 3 AWD Dual Motor'
models = [s, x, three]*3
dates = ['Sept 1 9am']*3 + ['Sept 1 10am']*3 + ['Sept 1 11am']*3
acceleration_times = [2.5, 2.92, 3.33, 2.51, 2.91, 3.31, 2.51, 2.92, 3.3]
data = {'car_model': models,
'date': dates,
'0-60mph_in_seconds': acceleration_times}
df = pd.DataFrame(data)
df
car_model | date | 0-60mph_in_seconds | |
---|---|---|---|
0 | Tesla Model S P100D | Sept 1 9am | 2.50 |
1 | Tesla Model X P100D | Sept 1 9am | 2.92 |
2 | Tesla Model 3 AWD Dual Motor | Sept 1 9am | 3.33 |
3 | Tesla Model S P100D | Sept 1 10am | 2.51 |
4 | Tesla Model X P100D | Sept 1 10am | 2.91 |
5 | Tesla Model 3 AWD Dual Motor | Sept 1 10am | 3.31 |
6 | Tesla Model S P100D | Sept 1 11am | 2.51 |
7 | Tesla Model X P100D | Sept 1 11am | 2.92 |
8 | Tesla Model 3 AWD Dual Motor | Sept 1 11am | 3.30 |
I want to pivot this data so each row is a unique car model, the columns are dates and the values in the table are the acceleration speeds.
df.pivot(index='car_model', columns='date', values='0-60mph_in_seconds')
date | Sept 1 10am | Sept 1 11am | Sept 1 9am |
---|---|---|---|
car_model | |||
Tesla Model 3 AWD Dual Motor | 3.31 | 3.30 | 3.33 |
Tesla Model S P100D | 2.51 | 2.51 | 2.50 |
Tesla Model X P100D | 2.91 | 2.92 | 2.92 |
This format may be easier to read so you can easily focus your attention on just the acceleration times for the 3 models.
You can read more about pandas pivot()
on the official documentation page.