A header necessarily stores the names or headings for each of the columns. It basically helps the user to identify the role of the respective column in the data frame. The top row containing column names is called the header row of the data frame. There are basically two approaches to add a header row in Python in case the original data frame doesn’t have a header.
Creating a data frame from CSV file and creating row header
While reading the data and storing it in a data frame, or creating a fresh data frame , column names can be specified by using the names attribute of the read_csv() method in Python. Names attribute contains an array of names for each of the columns of the data frame in order. The length of the array is equivalent to the length of this frame structure.
Python3
import pandas as pd
data_frame = pd.read_csv("test.txt", sep='\t',
names=['Name', 'Age', 'Profession'])
print("Data frame")
print(data_frame)
print("Row header")
print(list(data_frame.columns))
|
Output:

add header row to a Pandas Dataframe
We can also specify the header=none as an attribute of the read_csv() method and later on give names to the columns explicitly when desired.
Python3
import pandas as pd
data_frame = pd.read_csv("test.txt")
print("Original Data frame")
print(data_frame)
data_frame_new = pd.read_csv("test.txt", names=['A', 'B', 'C'])
print("New Data frame")
print(data_frame_new)
print("Row header")
print(list(data_frame_new.columns))
|
Output:

add header row to a Pandas Dataframe
Originally, the rows are numbered by index numbers beginning from 0, in case the CSV file does not have any row header.
Creating a data frame and creating row header in Python itself
We can create a data frame of specific number of rows and columns by first creating a multi -dimensional array and then converting it into a data frame by the pandas.DataFrame() method. The columns argument is used to specify the row header or the column names. It contains an array of column values with its length equal to the number of columns in the data frame.
Python3
import pandas as pd
data = [['Mallika', 23, 'Student'], [
'Yash', 25, 'Tutor'], ['Abc', 14, 'Clerk']]
data_frame = pd.DataFrame(data, columns=['Name', 'Age', 'Profession'])
print("Data frame")
print(data_frame)
print("Row header")
print(list(data_frame.columns))
|
Output:

add header row to a Pandas Dataframe
Creating a data frame from CSV file and Using set_axis() Method
We create a data frame of specific number of rows and columns by first creating a multi -dimensional array and then converting it into a data frame by the pandas.DataFrame() method. The set_axis() methods argument is used to specify the row header or the column names. It contains an array of column values with its length equal to the number of columns in the data frame.
Python3
import pandas as pd
data_frame=pd.read_csv("C:\\Users\\DELL\\Downloads\\data.tsv",
sep='\t')
print("Data frame")
print(data_frame)
data_frame.set_axis(["t_id", "Avg_rating", "votes"],
axis=1,inplace=True)
print()
print(data_frame)
print("Row header")
print(list(data_frame.columns))
|
Output:

add header row to a Pandas Dataframe
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!