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
- How to Reset Column Names (Index) in Pandas
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:
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:
.rename(columns=) expects to be iterable with the column names. To select the first row we are going to use
Finally we need to drop the first row which was used as a header by
The result is:
For other rows we can change the index - 0. For example to use the last row as header: -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:
Use the column header from the first row of the existing DataFrame.
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
In this short post we saw how to use a row as a header in Pandas. We covered also several Pandas methods like: