Convert column Values to Columns in a Pandas DataFrame

avatar
Borislav Hadzhiev

Last updated: Jul 12, 2023
5 min

banner

# Table of Contents

  1. Convert column Values to Columns in a Pandas DataFrame
  2. Convert column Values to Columns in a Pandas DataFrame using pivot
  3. Convert column Values to Columns in a Pandas DataFrame using groupby() and unstack()

# Convert column Values to Columns in a Pandas DataFrame

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.

main.py
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.

shell
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

convert column values to columns in pandas dataframe

We passed the following arguments to the DataFrame.pivot_table method:

  1. values - the column (or multiple columns in a list) to aggregate.
  2. index - the index column.
  3. columns - the column values that you want to use as column headers.
  4. aggfunc - the aggregation function.

# Convert column Values to Columns in a Pandas DataFrame using pivot

You can also use the DataFrame.pivot method to convert the column values of a DataFrame to columns.

main.py
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.

shell
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

convert column values to columns using pivot

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.

main.py
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.

shell
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

# Convert column Values to Columns in a Pandas DataFrame using groupby() and unstack()

You can also use the groupby() and unstack() methods to convert the column values in a DataFrame to columns.

main.py
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.

shell
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

convert column values to columns with groupby and unstack

The code sample:

  1. Resets the DataFrame index using reset_index().
main.py
# index id Frontend Styles # 0 0 1 React Bootstrap # 1 1 2 Vue Vuetify # 2 2 3 Angular Material print(df.reset_index())
  1. The groupby method groups the DataFrame by a series of columns.
  2. We then aggregate using DataFrame.first.
  3. The DataFrame.unstack method returns a 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.

main.py
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.

shell
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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.