Skip to content
Related Articles

Related Articles

Improve Article

How to add header row to a Pandas Dataframe?

  • Last Updated : 17 Jun, 2021

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.

Method 1: 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. 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




# pandas package is required
import pandas as pd
 
# converting csv file to data frame
data_frame = pd.read_csv("test.txt", sep='\t',
                         names=['Name', 'Age', 'Profession'])
 
 
# printing data frame
print("Data frame")
print(data_frame)
 
# printing row header
print("Row header")
print(list(data_frame.columns))

 Output:



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




# pandas package is required
import pandas as pd
 
# declaring a data frame  with three rowsand three columns
data_frame = pd.read_csv("test.txt")
 
# printing data frame
print("Original Data frame")
print(data_frame)
 
# adding column names
data_frame_new = pd.read_csv("test.txt", names=['A', 'B', 'C'])
print("New Data frame")
print(data_frame_new)
 
# printing row header
print("Row header")
print(list(data_frame_new.columns))

Output:

Originally, the rows are numbered by index numbers beginning from 0, in case the CSV file does not have any row header. 

Method 2: 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




# pandas package is required
import pandas as pd
 
# declaring a data frame  with three rowsand three columns
data = [['Mallika', 23, 'Student'], [
    'Yash', 25, 'Tutor'], ['Abc', 14, 'Clerk']]
 
# creating a pandas data frame
data_frame = pd.DataFrame(data, columns=['Name', 'Age', 'Profession'])
 
# printing data frame
print("Data frame")
print(data_frame)
 
# printing row header
print("Row header")
 
print(list(data_frame.columns))

 Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :