Python supports a variety of modules and functions for executing data analysis and manipulation operations. The data structure in Pandas named “DataFrame” is used in Python to store and manipulate data. The header of a DataFrame provides the column names, making it easy to identify and access the data. To add a header to DataFrame, various methods are utilized in Python.
This Python blog presents a detailed guide on adding header rows to Pandas DataFrame using numerous examples.
How to Add Header to Pandas DataFrame?
The following methods are utilized in Python to add/insert a header to a DataFrame:
- Using “pd.DataFrame()” Columns Parameter
- Using “DataFrame.columns” Method
- Using “DataFrame.set_axis()” Method
Add Header to Pandas DataFrame Using “pd.DataFrame()” Columns Parameter
The “pd.DataFrame()” method takes the “columns” as an argument and adds the header rows to the newly created DataFrame. For example, in the below code, the “pd.DataFrame()” method creates a DataFrame with header column values as “Name”, “Age”, and “Height”.
data1 = [ ["Joseph",20, 5.3],["Henry",25, 4.6],["Lily", 32, 4.7]]
column_names=["Name", "Age", "Height"]
print(pandas.DataFrame(data1, columns=column_names))
The above code retrieves the following DataFrame to the output:
Add Header to Pandas DataFrame Using “DataFrame.columns” Method
The “DataFrame.columns()” method can also be utilized to add header rows to the Pandas DataFrame. The following code adds the specified header column to the input DataFrame containing no header:
df = pandas.DataFrame([ ["Joseph",20, 5.3],["Henry",25, 4.6],["Lily", 32, 4.7]])
column_names=["Name", "Age", "Height"]
df.columns = column_names
print(df)
The above code execution generates the following DataFrame:
Add Header to Pandas DataFrame Using “DataFrame.set_axis()” Method
In Python, the “set_axis()” method can be used to change the labels of a DataFrame. We can change the labels of the columns or the rows by assigning a list of labels to the label’s argument.
This method is used in the below code to add header rows to the newly created DataFame by taking the list of header values and axis as an argument. The “axis” argument indicates which axis the labels will be assigned to. The value “0” specifies the rows, and “1” specifies the columns.
df = pandas.DataFrame([ ["Joseph",20, 5.3],["Henry",25, 4.6],["Lily", 32, 4.7]])
column_names=["Name", "Age", "Height"]
df = df.set_axis(column_names, axis=1)
print(df)
The above code displays the following output:
Add Multiple Header to Pandas DataFrame
To add multiple headers to Pandas DataFrame, the “pandas.MultiIndex.from_tuples()” method is used along with the “df.columns” method. In the below code, the “pandas.DataFrame()” method creates the DataFrame with header rows. After that, the “pandas.MultiIndex.from_tuples()” method creates the multi-index of the DataFrame.
df = pandas.DataFrame([ ["Joseph",20, 5.3],["Henry",25, 4.6],["Lily", 32, 4.7]],columns=["Name", "Age", "Height"])
df.columns = pandas.MultiIndex.from_tuples(zip(['A', 'B', 'C'], df.columns))
print(df)
The above code generates the below output:
That’s all about adding the header to the Pandas data frame.
Conclusion
The “pd.DataFrame()” columns parameter, “DataFrame.columns” method, and the “DataFrame.set_axis()” method is used to add a header to Pandas DataFrame in Python. These methods can be used to add a header while creating DataFrame or after creating the DataFrame. We can also add multiple headers to Pandas DataFrame using the “pandas.MultiIndex.from_tuples()” and the “df.columns” methods. This guide delivered a comprehensive tutorial on how to add a header to Pandas DataFrame using numerous examples.