Ignore Header when Reading CSV File as pandas DataFrame in Python (Example)

 

This article illustrates how to remove the header when reading a CSV file in the Python programming language.

Table of contents:

So now the part you have been waiting for – the example!

 

Example Data & Software Libraries

First, we have to import the pandas library.

import pandas as pd                                   # Import pandas library in Python

Next, let’s also create some exemplifying data in Python:

data = pd.DataFrame({'x1':['x', 'y', 'x', 'y', 'x'],  # Create pandas DataFrame
                     'x2':['a', 'b', 'c', 'd', 'e'],
                     'x3':['foo', 'bar', 'bar', 'foo', 'bar']})
print(data)                                           # Print pandas DataFrame

 

table 1 DataFrame ignore header when reading csv file as pandas dataframe python

 

As you can see based on Table 1, our example data is a pandas DataFrame and contains five rows and three columns. The names of these columns are x1, x2, and x3.

Let’s write these data to a CSV file in the current working directory on our computer:

data.to_csv('data.csv', index = False)                # Export pandas DataFrame to CSV

At this point, a new CSV file should appear in the folder that is used as your current working directory. This CSV file will be used as a basis for the following example.

 

Example: Skip Header when Reading CSV File as pandas DataFrame

In this example, I’ll explain how to remove the header when importing a CSV file as a pandas DataFrame.

For this task, we can apply the read_csv function as shown below. Within the read_csv function, we have to set the skiprows argument to be equal to 1.

data_import = pd.read_csv('data.csv',                 # Read pandas DataFrame from CSV
                          skiprows = 1)
print(data_import)                                    # Print imported pandas DataFrame

 

table 2 DataFrame ignore header when reading csv file as pandas dataframe python

 

By running the previous Python syntax, we have constructed Table 2, i.e. a new pandas DataFrame. In this DataFrame, the original header of the input CSV has been ignored, and the first row of the input data has been set as a header.

 

Video & Further Resources

I have recently released a video on my YouTube channel, which illustrates the Python programming code of this article. You can find the video below:

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at the other Python tutorials on my homepage. I have published several posts already:

 

In this article you have learned how to skip the header when reading a CSV file in Python programming. If you have additional comments and/or questions, don’t hesitate to let me know in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top