In this section, we will learn how to drop header row of Pandas DataFrame in Python. In this Python tutorial, we will create the header row then we will drop it. And also we will go through every topic of dropping the header row of Pandas DataFrame with examples in Python.
Moreover, we will also cover the following topics in this Python Pandas tutorial:
- Drop header row of Pandas DataFrame
- Drop the header row using the skiprows parameter in Python
- Drop the header row while reading the Pandas DataFrame in Python
Drop header row of Pandas DataFrame
There are mainly two ways to drop the header of Pandas DataFrame in Python. The two methods are by using the skiprows parameter after exporting the DataFrame to a CSV file and other is by setting the ‘False’ value to the index parameter in Python. To do this, we have to first create our DataFrame using the Pandas library.
Create Pandas DataFrame
Here we are trying to create our Pandas DataFrame in Python.
- Here We have created a dictionary of employee data that has the names of the employees, experiences, and roles of the employees from different organizations.
- Later it is passed to the “pandas.DataFrame” function in order to convert it to a data frame or a table i.e in the form of rows and columns.
#Importing the necessary libraries
import numpy as np
import pandas as pd
#Create a dictionary which has employee names, their experience, company and roles
data_dict={"Names":["Kelvin", "John", "smith", "Robin","Williams","Nick","Anyy","Messi","Jonas","Xavier"],
"Experience":[13,7,np.nan,9,0,12,21,3,9,17],
"Company": ["Google","Amazon","Google","Flipkart","Amazon","Google","Flipkart","Amazon","Google","Flipkart"],
"Role": ["IT Analyst","Software Engineer","Software Engineer","Data Analyst","Data Engineer","Data Scientist","ML Engineer","ML Engineer","ML Engineer","Data Scientist"]}
#Create a DataFrame using Pandas
Employee_data=pd.DataFrame(data_dict)
Employee_data
- Below is the pandas DataFrame that we have created in Python which has the employee names, their company name, roles, and experience in the IT Industry.
- The column names of the pandas DataFrame are given to the dictionary ‘data_dict’ as keys and the rest are values in Python.
Here we have created the Pandas DataFrame that has the employees’ data. Now let us understand different methods of dropping the header of Pandas DataFrames with examples in Python.
Drop Header row using skiprows parameter in Python
Right after creating the DataFrame, we have to export the Pandas DataFrame to a CSV file with the index parameter set to ‘False’ in order to remove the ‘Unnamed’ column of the Pandas DataFrame in Python.
- And then we can read the CSV file (Employee_datamodified) by calling the function ‘pd.read_csv()‘ from the Pandas library in Python.
- While reading the CSV file we have to pass the value of 1 to the parameter skiprows to the function ‘pd.read_csv()’ in order to remove the header row or the column names of a Pandas DataFrame.
- The skiprows parameter will skip the first row i.e header of the Pandas DataFrame in Python.
#Export the DataFrame to CSV file
Employee_data.to_csv('Employee_datamodified',index=False)
#Print the Pandas DataFrame that is exported
Employee_datamodified=pd.read_csv('Employee_datamodified',skiprows=1)
Employee_datamodified
In the below output image, we can observe that the column names of the Pandas DataFrame have been dropped and instead the first row elements have taken the place of the header row.
This is how can drop the header row of Pandas DataFrame using the skiprows parameter in Python.
Drop Header row while reading Pandas DataFrame
Right after creating the Pandas DataFrame, we have to export the DataFrame to a CSV file with the index parameter set to ‘False’ in order to remove the ‘Unnamed’ column of the Pandas DataFrame in Python.
- And then we can read the CSV file (Employee_datamodified) by calling the function ‘pd.read_csv()‘ from the Pandas library in Python.
- While reading the CSV file we have to pass the value of None to the parameter header to the function ‘pd.read_csv()’ in order to remove or ignore the header row or the column names of a Pandas DataFrame.
- The header parameter when set to None will ignore the header of the Pandas DataFrame in Python.
#Export the DataFrame to csv file
Employee_data.to_csv('Employee_datamodified',index=False)
#Print the Pandas DataFrame that is exported
Employee_datamodified=pd.read_csv('Employee_datamodified',header=None)
Employee_datamodified
In the below output image, we can observe that the column names of the DataFrame have been ignored and instead the column names took the place of the first row elements.
This is how we can drop the header row while reading Pandas DataFrame in Python.
Conclusion
In this Python Pandas tutorial, We have covered the topics related to header rows i.e different methods to drop the header rows from a Pandas DataFrame in Python:
- One is by using the skiprows parameter in Python Pandas
- And the other is by setting the None value to the Header parameter while exporting the Pandas DataFrame to a CSV file
Also, we saw examples for every topic of dropping the header row of Pandas DataFrame in Python. For a better understanding of Python Pandas, we can follow the below Python tutorials.
- Get column index from column name of Pandas DataFrame
- How to get Index values of Pandas DataFrames
- Drop non-numeric columns from pandas DataFrame
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.