Saturday, September 21, 2024
Google search engine
HomeLanguagesHow to add header row to a Pandas Dataframe?

How to add header row to a Pandas Dataframe?

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




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

add header row to a Pandas Dataframe

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




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

add header row to a Pandas Dataframe

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




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

add header row to a Pandas Dataframe

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
# converting csv file to data frame
 
data_frame=pd.read_csv("C:\\Users\\DELL\\Downloads\\data.tsv",
                         sep='\t')
 
# printing data frame
print("Data frame")
print(data_frame)
 
data_frame.set_axis(["t_id", "Avg_rating", "votes"],
                    axis=1,inplace=True)
print()
print(data_frame)
 
# printing row header
print("Row header")
print(list(data_frame.columns))


Output:

add header row to a Pandas Dataframe

add header row to a Pandas Dataframe

RELATED ARTICLES

Most Popular

Recent Comments