1. Overview

This guide describes how to convert first or other rows as a header in Pandas DataFrame.

We will cover several different examples with details.

If you are using read_csv() method you can learn more

2. Setup

We are going to work with simple DataFrame created by:

import pandas as pd
df = pd.DataFrame([('head_1', 'head_2', 'head_3' ),
                   ('val_11','val_12','val_13'),
                   ('val_21','val_22','val_23'),
                  ('val_31','val_32','val_33')])

Final DataFrame looks like:

0 1 2
0 head_1 head_2 head_3
1 val_11 val_12 val_13
2 val_21 val_22 val_23
3 val_31 val_32 val_33

From this DataFrame we can conclude that the first row of it should be used as a header.

3. Using First Row as a Header with df.rename()

The first solution is to combine two Pandas methods:

The method .rename(columns=) expects to be iterable with the column names. To select the first row we are going to use iloc - df.iloc[0].

Finally we need to drop the first row which was used as a header by drop(df.index[0]):

df.rename(columns=df.iloc[0]).drop(df.index[0])

The result is:

head_1 head_2 head_3
1 val_11 val_12 val_13
2 val_21 val_22 val_23
3 val_31 val_32 val_33

For other rows we can change the index - 0. For example to use the last row as header: -1 - df.iloc[-1].

To make the change permanent we need to use inplace = True or reassign the DataFrame.

4. Using First Row as a Header with pd.DataFrame()

Another solution is to create new DataFrame by using the values from the first one - up to the first row: df.values[1:]

Use the column header from the first row of the existing DataFrame.

pd.DataFrame(df.values[1:], columns=df.iloc[0])

The result is exactly the same as the previous solution.

There are several differences:

  • This solution might be slower for bigger DataFrames
  • It creates new DataFrame
  • It may change the dtypes of the new DataFrame

5. Conclusion

In this short post we saw how to use a row as a header in Pandas. We covered also several Pandas methods like: iloc(), rename() and drop()