How to skip rows while reading csv file using Pandas?

27 Aug, 2021
Python is a good language for doing data analysis because of the amazing ecosystem of data-centric python packages. Pandas package is one of them and makes importing and analyzing data so much easier.
Here, we will discuss how to skip rows while reading csv file. We will use read_csv() method of Pandas library for this task.
Syntax: pd.read_csv(filepath_or_buffer, sep=’, ‘, delimiter=None, header=’infer’, names=None, index_col=None, usecols=None, squeeze=False, prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skipinitialspace=False, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=False, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, iterator=False, chunksize=None, compression=’infer’, thousands=None, decimal=b’.’, lineterminator=None, quotechar='”‘, quoting=0, escapechar=None, comment=None, encoding=None, dialect=None, tupleize_cols=None, error_bad_lines=True, warn_bad_lines=True, skipfooter=0, doublequote=True, delim_whitespace=False, low_memory=True, memory_map=False, float_precision=None)  
Some useful parameters are given below :  
For downloading the student.csv file Click Here
Method 1: Skipping N rows from the starting while reading a csv file. 
Code:  
Python3
# Importing Pandas libraryimport pandas as pd # Skipping 2 rows from start in csv# and initialize it to a  dataframedf = pd.read_csv("students.csv",                  skiprows = 2) # Show the dataframedf
Output :  
Method 2: Skipping rows at specific positions while reading a csv file. 
Code: 
Python3
# Importing Pandas libraryimport pandas as pd # Skipping rows at specific positiondf = pd.read_csv("students.csv",                  skiprows = [0, 2, 5]) # Show the dataframedf
Output :  
Method 3: Skipping N rows from the starting except column names while reading a csv file. 
Code:  
Python3
# Importing Pandas libraryimport pandas as pd # Skipping 2 rows from start# except the column namesdf = pd.read_csv("students.csv",                 skiprows = [i for i in range(1, 3) ]) # Show the dataframedf
Output :  
Method 4: Skip rows based on a condition while reading a csv file. 
Code:  
Python3
# Importing Pandas libraryimport pandas as pd # function for checking and# skipping every 3rd linedef logic(index):     if index % 3 == 0:        return True     return False # Skipping rows based on a conditiondf = pd.read_csv("students.csv",                 skiprows = lambda x: logic(x) ) # Show the dataframedf
Output :  
Method 5: Skip N rows from the end while reading a csv file. 
Code:  
Python3
# Importing Pandas libraryimport pandas as pd # Skipping 2 rows from enddf = pd.read_csv("students.csv",                  skipfooter = 5,                  engine = 'python') # Show the dataframedf
Output :  
 
simmytarika5
surinderdawra388
sweetyty
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Create a directory in Python