Pivot#
# general syntax
DataFrame.pivot(self, index=None, columns=None, values=None)
Parameters:
indexColumn that makes the new frame’s index; if
None, existing index is used
columnsExisting column used to make new column(s)
valuesExisting 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
fffcolumn will become the row indexValues in the
bbbcolumn will become the column indexValues in the
baacolumn will be mapped to the new row/column indexzzzcolumn 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:
fffcolumn values become the row indexbbbcolumn values become the column indexbaaANDzzzcolumn 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