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