Remove First Row when Importing pandas DataFrame from CSV File in Python (Example Code)

In this post you’ll learn how to delete the first row when loading a pandas DataFrame from a CSV file in Python.

Preparing the Example

import pandas as pd                           # Import pandas library in Python
my_df = pd.DataFrame({'A':range(100, 105),    # Construct new pandas DataFrame
                      'B':[9, 3, 7, 1, 2],
                      'C':['a', 'f', 'c', 'f', 'f'],
                      'D':range(35, 30, - 1)})
print(my_df)
#      A  B  C   D
# 0  100  9  a  35
# 1  101  3  f  34
# 2  102  7  c  33
# 3  103  1  f  32
# 4  104  2  f  31
my_df.to_csv('my_df.csv')                     # Write exemplifying DataFrame to folder

Example: Ignore First Row when Reading a CSV File

my_df_new = pd.read_csv('my_df.csv',          # Import pandas DataFrame from CSV
                        skiprows = [1])
print(my_df_new)
#    Unnamed: 0    A  B  C   D
# 0           1  101  3  f  34
# 1           2  102  7  c  33
# 2           3  103  1  f  32
# 3           4  104  2  f  31

Related Tutorials

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

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top