Last updated: Apr 12, 2024
Reading time·5 min
Use the DataFrame.pivot_table()
method to convert column values to columns
in a Pandas DataFrame
.
The method will return a spreadsheet-style pivot table as a DataFrame
.
import pandas as pd df = pd.DataFrame({ 'Frontend': ['React', 'Vue', 'Angular'], 'Styles': ['Bootstrap', 'Vuetify', 'Material'], }) print(df) print('-' * 50) df2 = df.pivot_table(values='Styles', index=df.index, columns='Frontend', aggfunc='first') print(df2)
Running the code sample produces the following output.
Frontend Styles 0 React Bootstrap 1 Vue Vuetify 2 Angular Material -------------------------------------------------- Frontend Angular React Vue 0 NaN Bootstrap NaN 1 NaN NaN Vuetify 2 Material NaN NaN
We passed the following arguments to the DataFrame.pivot_table method:
values
- the column (or multiple columns in a list) to aggregate.index
- the index column.aggfunc
- the aggregation function.pivot
You can also use the
DataFrame.pivot()
method to convert the column values of a DataFrame
to columns.
import pandas as pd df = pd.DataFrame({ 'id': [1, 2, 3], 'Frontend': ['React', 'Vue', 'Angular'], 'Styles': ['Bootstrap', 'Vuetify', 'Material'], }) print(df) print('-' * 50) df2 = df.pivot(index='id', columns='Frontend', values='Styles') print(df2)
Running the code sample produces the following output.
id Frontend Styles 0 1 React Bootstrap 1 2 Vue Vuetify 2 3 Angular Material -------------------------------------------------- Frontend Angular React Vue id 1 NaN Bootstrap NaN 2 NaN NaN Vuetify 3 Material NaN NaN
The DataFrame.pivot
method returns a reshaped DataFrame
that is organized by
the given index/column values.
We could've also accessed the Styles
column using bracket notation instead of
supplying the values
argument.
import pandas as pd df = pd.DataFrame({ 'id': [1, 2, 3], 'Frontend': ['React', 'Vue', 'Angular'], 'Styles': ['Bootstrap', 'Vuetify', 'Material'], }) print(df) print('-' * 50) df2 = df.pivot(index='id', columns='Frontend')['Styles'] print(df2)
Running the code sample produces the following output.
id Frontend Styles 0 1 React Bootstrap 1 2 Vue Vuetify 2 3 Angular Material -------------------------------------------------- Frontend Angular React Vue id 1 NaN Bootstrap NaN 2 NaN NaN Vuetify 3 Material NaN NaN
groupby()
and unstack()
You can also use the groupby()
and unstack()
methods to convert the column
values in a DataFrame
to columns.
import pandas as pd df = pd.DataFrame({ 'id': [1, 2, 3], 'Frontend': ['React', 'Vue', 'Angular'], 'Styles': ['Bootstrap', 'Vuetify', 'Material'], }) print(df) print('-' * 50) df2 = df.reset_index().groupby(['id', 'Frontend'])[ 'Styles'].aggregate('first').unstack() print(df2)
Running the code sample produces the following output.
id Frontend Styles 0 1 React Bootstrap 1 2 Vue Vuetify 2 3 Angular Material -------------------------------------------------- Frontend Angular React Vue id 1 NaN Bootstrap NaN 2 NaN NaN Vuetify 3 Material NaN NaN
The code sample:
DataFrame
index using
reset_index().# index id Frontend Styles # 0 0 1 React Bootstrap # 1 1 2 Vue Vuetify # 2 2 3 Angular Material print(df.reset_index())
DataFrame
by a series of columns.DataFrame
with a new level of column labels whose
innermost level consists of the pivoted index labels.You can achieve a similar result by using DataFrame.set_index().
import pandas as pd df = pd.DataFrame({ 'Frontend': ['React', 'Vue', 'Angular'], 'Styles': ['Bootstrap', 'Vuetify', 'Material'], }) print(df) print('-' * 50) df2 = df.set_index([df.index, 'Frontend'])['Styles'].unstack() print(df2)
Running the code sample produces the following output.
Frontend Styles 0 React Bootstrap 1 Vue Vuetify 2 Angular Material -------------------------------------------------- Frontend Angular React Vue 0 NaN Bootstrap NaN 1 NaN NaN Vuetify 2 Material NaN NaN
You can learn more about the related topics by checking out the following tutorials: