Last updated: Apr 11, 2024
Reading time·6 min
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.
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)
Running the code sample produces the following output.
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
Notice that there is a duplicate row.
You can use the df.drop()
method to drop the duplicate.
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)
Running the code sample produces the following output.
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.
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)
Running the code sample produces the following output.
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.
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)
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.
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.
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.
DataFrame.rename()
method to convert a row to a column headerYou can also use the DataFrame.rename method to convert a row to a column header in Pandas.
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)
Running the code sample produces the following output.
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
If you want to delete the duplicate row, use the drop()
method.
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)
Running the code sample produces the following output.
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
You can also restart the index after removing the duplicate row.
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)
Running the code sample produces the following output.
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
.
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)
Running the code sample above produces the following output.
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
You can also convert a row to a column header by creating a new DataFrame.
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)
Running the code sample produces the following output.
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.
You can learn more about the related topics by checking out the following tutorials:
pd.read_json()