pandas.read_csv() – Read CSV with Pandas In Python

Pandas is a popular 3rd party Python module and library used for statistical and related arithmetic calculations. The pandas module provides read_csv() function which is used to read a CSV file. CSV file is a comma-separated file that is used in a structured way. Alternatively, different separators can be used with the read_csv() method.

read_csv() Method Syntax

The read_csv() method accepts a lot of different parameters. The CSV file name or path is required and other parameters are optional.

read_csv(CSV_FILE,OPTIONS)
  • CSV_FILE is the CSV file name or complete path.
  • OPTIONS is single or more options related to reading and parsing a CSV file. This is optional.

Read CSV File

The read_csv() method can be used to read a CSV file where every column is separated from the columns by default. In the following example, we read the CSV file named “data.csv”.

# Import pandas module
import pandas as pd
 
# reading data.csv file
pd.read_csv("data.csv")

Alternatively, we can read a CSV file by using its absolute or complete path like below.

# Import pandas module
import pandas as pd
 
# reading data.csv file
pd.read_csv("/home/ismail/data.csv")

Use Different Separator

By default, a comma is used as a separator. The separator is defined as a comma implicitly. But we can use different separators. These separators can be single or more characters. The separator characters can be a letter or sign. The sep parameter is used as the separator parameter for the csv_read() method. In the following example we set the separator as “;”

# Import pandas module
import pandas as pd
 
# reading data.csv file
pd.read_csv("data.csv",sep=";")

Leave a Comment