> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Convert First Row to  Header Column in Pandas DataFrame
- URL: https://datascientyst.com/convert-first-row-header-column-pandas-dataframe/
- Published: 2022-02-04T10:12:16.000Z
- Updated: 2022-02-04T16:09:20.000Z
- Author: John D K
- Tags: Row

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

- about headers: [How to Read Excel or CSV With Multiple Line Headers Using Pandas ](https://datascientyst.com/read-excel-csv-multiple-line-headers-using-pandas/)
- [How to Reset Column Names (Index) in Pandas](https://datascientyst.com/reset-column-names-index-pandas/)

## 2\. Setup

We are going to work with simple DataFrame created by:

```python
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:

- [pandas.DataFrame.rename](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename.html?ref=datascientyst.com)
- [pandas.DataFrame.drop](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.drop.html?ref=datascientyst.com)

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])`:

```python
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.

```python
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()`