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

Method 3: Using numpy

You can also use the numpy python package to remove the header from the dataframe. Here you will create a new dataframe after removing the header. You will pass the df[1:] and columns=df.columns as arguments for the function np.array() function. The first argument will create a new dataframe without the header and the second argument retain the original column from the existing dataframe. Use the below lines of code to implement this method.

import numpy as np
import pandas as pd

data = {'Name': ['Sukesh', 'Abishek', 'Rob'],
        'Age': [28, 24, 22],
        'City': ['New York', 'San Francisco', 'Delhi']}

df = pd.DataFrame(data)

# Remove headers
df_no_headers = pd.DataFrame(np.array(df[1:]), columns=df.columns)

print(df_no_headers)

Output

The output will be same as the above.

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.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner