Convert a Row to a Column Header in a Pandas DataFrame

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
6 min

banner

# Convert a Row to a Column Header in a Pandas DataFrame

Set the column property to the result of accessing the iloc indexer at the given index to convert a row to a column header in a Pandas DataFrame.

The iloc indexer is used for integer, location-based indexing for selection by position.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df) df.columns = df.iloc[1] print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- 1 Bobby 13 180.2 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3

convert row to column header in pandas dataframe

Notice that there is a duplicate row.

You can use the df.drop() method to drop the duplicate.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df) df.columns = df.iloc[1] print('-' * 50) print(df) df = df.drop(df.index[1]) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- 1 Bobby 13 180.2 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- 1 Bobby 13 180.2 0 Alice 10 175.1 2 Carl 15 190.3

You can also reset the index by using the DataFrame.reset_index() method.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df) df.columns = df.iloc[1] print('-' * 50) print(df) df = df.drop(df.index[1]) print('-' * 50) print(df) df = df.reset_index(drop=True) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- 1 Bobby 13 180.2 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- 1 Bobby 13 180.2 0 Alice 10 175.1 2 Carl 15 190.3 -------------------------------------------------- 1 Bobby 13 180.2 0 Alice 10 175.1 1 Carl 15 190.3

The df.drop(df.index[1]) method call removes all rows that have the same label as the second row (index 1).

If the index is not unique, use a RangeIndex object.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df) df.columns = df.iloc[1] print('-' * 50) print(df) df = df.iloc[pd.RangeIndex(len(df)).drop(1)] print('-' * 50) print(df)
The code for this article is available on GitHub

We called the drop() method on the RangeIndex object to delete the row at index 1.

The DataFrame.iloc indexer returns the row at the given index.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df.iloc[1]) print('-' * 50) print(df.iloc[2])

Running the code sample produces the following output.

shell
name Bobby experience 13 salary 180.2 Name: 1, dtype: object -------------------------------------------------- name Carl experience 15 salary 190.3 Name: 2, dtype: object

Note that the .iloc indexer raises an IndexError if the supplied index is out of bounds.

# Using the DataFrame.rename() method to convert a row to a column header

You can also use the DataFrame.rename method to convert a row to a column header in Pandas.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df) df = df.rename(columns=df.iloc[1]) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- Bobby 13 180.2 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3

convert row to column header using rename

If you want to delete the duplicate row, use the drop() method.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df) df = df.rename(columns=df.iloc[1]) print('-' * 50) print(df) df = df.drop(df.index[1]) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- Bobby 13 180.2 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- Bobby 13 180.2 0 Alice 10 175.1 2 Carl 15 190.3

delete duplicate row using drop

You can also restart the index after removing the duplicate row.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df) df = df.rename(columns=df.iloc[1]) print('-' * 50) print(df) df = df.drop(df.index[1]) print('-' * 50) print(df) df = df.reset_index(drop=True) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- Bobby 13 180.2 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- Bobby 13 180.2 0 Alice 10 175.1 2 Carl 15 190.3 -------------------------------------------------- Bobby 13 180.2 0 Alice 10 175.1 1 Carl 15 190.3

Notice that the index starts at 0 after calling DataFrame.reset_index().

If you want to convert a given row to a column header without reassigning the DataFrame, set the inplace argument to True.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df) df.rename(columns=df.iloc[1], inplace=True) print('-' * 50) print(df) df.drop(df.index[1], inplace=True) print('-' * 50) print(df) df.reset_index(drop=True, inplace=True) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample above produces the following output.

shell
name experience salary 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- Bobby 13 180.2 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- Bobby 13 180.2 0 Alice 10 175.1 2 Carl 15 190.3 -------------------------------------------------- Bobby 13 180.2 0 Alice 10 175.1 1 Carl 15 190.3

# Convert a row to a column header by creating a new DataFrame

You can also convert a row to a column header by creating a new DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'experience': [10, 13, 15], 'salary': [175.1, 180.2, 190.3], }) print(df) headers = df.iloc[0] new_df = pd.DataFrame(df.values[1:], columns=headers) print('-' * 50) print(new_df) df.reset_index(drop=True, inplace=True) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3 -------------------------------------------------- 0 Alice 10 175.1 0 Bobby 13 180.2 1 Carl 15 190.3 -------------------------------------------------- name experience salary 0 Alice 10 175.1 1 Bobby 13 180.2 2 Carl 15 190.3

We created a new DataFrame and set the columns to the first row (index 0) by supplying the columns keyword argument.

You can optionally use the reset_index method to reset the index of the DataFrame and use the default index instead.

# 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.