Skip First Row when Reading pandas DataFrame from CSV File in Python (Example)

 

This article illustrates how to remove the first row when importing a pandas DataFrame from a CSV file in the Python programming language.

The content of the post is structured as follows:

Let’s take a look at some Python codes in action!

 

Example Data & Add-On Libraries

We first need to import the pandas library, in order to use the corresponding functions:

import pandas as pd                         # Load pandas library

Next, we also need to create some example data.

data = pd.DataFrame({'x1':range(21, 27),    # Create pandas DataFrame
                     'x2':['a', 'b', 'c', 'd', 'e', 'f'],
                     'x3':[4, 2, 8, 3, 7, 8],
                     'x4':[5, 9, 1, 7, 4, 7]})
print(data)                                 # Print pandas DataFrame

 

table 1 DataFrame skip first row when reading pandas dataframe from csv file python

 

As you can see based on Table 1, our example data is a pandas DataFrame constituted of six rows and four columns.

Now, we can create an exemplifying CSV file containing this DataFrame by using the to_csv function as shown below:

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

Once the previous Python code was executed, a new CSV file appears in your current working directory. This CSV file will be used as a basis for the following example.

 

Example: Skip First Row when Reading CSV File as pandas DataFrame

This section illustrates how to delete the very first row when importing a pandas DataFrame from a CSV file in Python.

For this task, we have to set the skiprows argument within the read_csv function to [1]. Consider the Python syntax below:

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

 

table 2 DataFrame skip first row when reading pandas dataframe from csv file python

 

As visualized in Table 2, we have managed to construct a new pandas DataFrame where the first row of our CSV file has been ignored. However, the header of our file has been kept, i.e. the column names are the same as in the input file.

 

Video & Further Resources

Do you need further information on the Python programming code of this article? Then you could watch the following video that I have published on my YouTube channel. In the video instruction, I’m illustrating the Python programming codes of this tutorial in a live session in the Python programming language.

 

The YouTube video will be added soon.

 

Furthermore, you may read the other articles on this homepage.

 

You have learned in this article how to skip the first row when importing a pandas DataFrame in Python. Please let me know in the comments section, in case you have additional questions or comments. Furthermore, don’t forget to subscribe to my email newsletter in order to receive updates on new tutorials.

 

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