Open In App

How to skip rows while reading csv file using Pandas?

Last Updated : 27 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 : 
 

Parameter Use
filepath_or_buffer URL or Dir location of file
sep Stands for separator, default is ‘, ‘ as in csv(comma separated values)
index_col This parameter is use to make passed column as index instead of 0, 1, 2, 3…r
header This parameter is use to make passed row/s[int/int list] as header
use_cols This parameter is Only uses the passed col[string list] to make data frame
squeeze If True and only one column is passed then returns pandas series
skiprows This parameter is use to skip passed rows in new data frame
skipfooter This parameter is use to skip Number of lines at bottom of file

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 library
import pandas as pd
 
# Skipping 2 rows from start in csv
# and initialize it to a  dataframe
df = pd.read_csv("students.csv",
                  skiprows = 2)
 
# Show the dataframe
df


Output : 
 

csv file content

Method 2: Skipping rows at specific positions while reading a csv file. 

Code: 

Python3




# Importing Pandas library
import pandas as pd
 
# Skipping rows at specific position
df = pd.read_csv("students.csv",
                  skiprows = [0, 2, 5])
 
# Show the dataframe
df


Output : 
 

csv file content_6

Method 3: Skipping N rows from the starting except column names while reading a csv file. 

Code:  

Python3




# Importing Pandas library
import pandas as pd
 
# Skipping 2 rows from start
# except the column names
df = pd.read_csv("students.csv",
                 skiprows = [i for i in range(1, 3) ])
 
# Show the dataframe
df


Output : 
 

csv file content_5

Method 4: Skip rows based on a condition while reading a csv file. 

Code:  

Python3




# Importing Pandas library
import pandas as pd
 
# function for checking and
# skipping every 3rd line
def logic(index):
 
    if index % 3 == 0:
        return True
 
    return False
 
# Skipping rows based on a condition
df = pd.read_csv("students.csv",
                 skiprows = lambda x: logic(x) )
 
# Show the dataframe
df


Output : 
 

csv file content_4

Method 5: Skip N rows from the end while reading a csv file. 

Code:  

Python3




# Importing Pandas library
import pandas as pd
 
# Skipping 2 rows from end
df = pd.read_csv("students.csv",
                  skipfooter = 5,
                  engine = 'python')
 
# Show the dataframe
df


Output : 
 

csv file content_3

 



Similar Reads

Reading specific columns of a CSV file using Pandas
CSV files are widely utilized for storing tabular data in file systems, and there are instances where these files contain extraneous columns that are irrelevant to our analysis. This article will explore techniques for selectively reading specific columns from a CSV file using Python. Let us see how to read specific columns of a CSV file using Pand
3 min read
How to create multiple CSV files from existing CSV file using Pandas ?
In this article, we will learn how to create multiple CSV files from existing CSV file using Pandas. When we enter our code into production, we will need to deal with editing our data files. Due to the large size of the data file, we will encounter more problems, so we divided this file into some small files based on some criteria like splitting in
3 min read
How to Remove Index Column While Saving CSV in Pandas
In this article, we'll discuss how to avoid pandas creating an index in a saved CSV file. Pandas is a library in Python where one can work with data. While working with Pandas, you may need to save a DataFrame to a CSV file. The Pandas library includes an index column in the output CSV file by default. Further in the article, we'll understand the d
3 min read
Uploading and Reading a CSV File in Flask
Flask is a flexible, lightweight web-development framework built using python. A Flask application is a Python script that runs on a web server, which listens to HTTP requests and returns responses. It is designed for simple and faster development. In this article, let's upload a CSV (Comma-Separated Values) file and read the contents of the file o
3 min read
Pandas - DataFrame to CSV file using tab separator
Let's see how to convert a DataFrame to a CSV file using the tab separator. We will be using the to_csv() method to save a DataFrame as a csv file. To save the DataFrame with tab separators, we have to pass "\t" as the sep parameter in the to_csv() method. Approach : Import the Pandas and Numpy modules.Create a DataFrame using the DataFrame() metho
1 min read
Convert Text File to CSV using Python Pandas
Let's see how to Convert Text File to CSV using Python Pandas. Python will read data from a text file and will create a dataframe with rows equal to number of lines present in the text file and columns equal to the number of fields present in a single line. See below example for better understanding. [caption width="800"]Original Text File[/caption
2 min read
Copying Csv Data Into Csv Files Using Python
CSV files, the stalwarts of information exchange, can be effortlessly harnessed to extract specific data or merge insights from multiple files. In this article, we unveil five robust approaches, transforming you into a virtuoso of CSV data migration in Python. Empower your data-wrangling endeavors, mastering the art of copying and organizing inform
4 min read
Create a GUI to convert CSV file into excel file using Python
Prerequisites: Python GUI – tkinter, Read csv using pandas CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma. Modules Needed Pandas: Python i
3 min read
How to convert CSV File to PDF File using Python?
In this article, we will learn how to do Conversion of CSV to PDF file format. This simple task can be easily done using two Steps : Firstly, We convert our CSV file to HTML using the PandasIn the Second Step, we use PDFkit Python API to convert our HTML file to the PDF file format. Approach: 1. Converting CSV file to HTML using Pandas Framework. P
3 min read
Reading and Writing CSV Files in Python
CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. It is one of the most common methods for exchanging data between applications and popular data format used in Data Science. It is supported by a wide range of applications. A CSV file stores tabular data in which each data field is separa
4 min read
Practice Tags :