How to Remove Header from Dataframe in Python : 3 Steps only

Remove Header from Dataframe in Python

The header or column name is also present when you read dataframe. What if you don’t want to include a header row from the data frame? In this post, you will know how to remove the header from dataframe in python with easy steps.

Steps to remove the header from Dataframe in Python

Let me know all the steps that you must follow for better understanding.  All the coding part has been done on Jupyter Notebook so you should also code on the same for better understanding.

Step 1: Import the necessary library

The first step is to import the required package that will be used in this tutorial. Import all the modules using the import statement.

import pandas as pd

Step 2: Create sample dataframe

In the second step, I will create a sample dataframe that will be used for implementing this example. However, you can use your own datasets for this example. You can convert the data to dataframe using the pd.DataFrame() constructor.

Run the below lines of code to create the sample dataframe.

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df

Output

sample dataframe for removing header from it
sample dataframe for removing header from it

Step 3: Use the below methods to Remove the Header from Dataframe in Python

Here you will know all the methods to remove headers from dataframe in Python.

Method 1: Use the drop() function

If the header of the dataframe contains an index of the columns then you can drop them using the drop() function. You have to pass the df.index[0] to remove the first row of the column.

df.drop(df.index[0])

Method 2: Using the read_csv() method

Suppose you are reading a CSV file or exporting the current sample dataframe to a CSV file and after that again reading it. Then you can easily ignore the header using the pd.read_csv() function. Here you have to pass the header=None parameter also with your CSV file.

Run the below lines of code to read and remove the header from dataframe.

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df.to_csv("data.csv")
df = pd.read_csv("data.csv",header=None)
df

Output

removing header from dataframe using read_csv() function
removing header from dataframe using read_csv() function

Conclusion

Sometimes you don’t want to include the header in the dataframe while reading the datasets. You can easily ignore or remove them using the above methods. In this tutorial, We have used drop() and read_csv() functions for that.

I hope you must have understood how to remove the header from dataframe. If you have any doubt then you can contact us for more information.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner