Pivot#

# general syntax
DataFrame.pivot(self, index=None, columns=None, values=None)

Parameters:

  • index

    • Column that makes the new frame’s index; if None, existing index is used

  • columns

    • Existing column used to make new column(s)

  • values

    • Existing column(s) that become values in newly-created columns; if no columns are specified, all columns are used, creating multi-index

Let’s start with a sample DataFrame.

import numpy as np, pandas as pd # import statements

# create df
df = pd.DataFrame({'fff': ['one', 'one', 'one', 'two', 'two',
                           'two'],
                   'bbb': ['P', 'Q', 'R', 'P', 'Q', 'R'],
                   'baa': [2, 3, 4, 5, 6, 7],
                   'zzz': ['h', 'i', 'j', 'k', 'l', 'm']})
df # show df

Example #1#

Now we’re going to use .pivot():

  • Values in the fff column will become the row index

  • Values in the bbb column will become the column index

  • Values in the baa column will be mapped to the new row/column index

  • zzz column will be dropped

# show pivot operation
df.pivot(index='fff', columns='bbb', values='baa')
# alternate syntax for the same pivot operation; uses bracket syntax to select baa column values
df.pivot(index='fff', columns='bbb')['baa']
# assign pivot output to new df
dfPivot = df.pivot(index='fff', columns='bbb', values='baa')

# show new df
dfPivot

Example #2#

An alternate .pivot() workflow for the original dataframe:

  • fff column values become the row index

  • bbb column values become the column index

  • baa AND zzz column values are mapped onto the new row/column index structure

# syntax for second pivot operation
df.pivot(index='fff', columns='bbb', values=['baa', 'zzz'])
# assign to new df
dfPivot2 = df.pivot(index='fff', columns='bbb', values=['baa', 'zzz'])

# show new df
dfPivot2

Additional Resources#