Skip to main content
Read-csv-using-pandas

Read CSV File Using Pandas read_csv()

This tutorial helps to read CSV file using pandas. We’ll use the pandas read_csv() method to read CSV file content. Pandas is the most popular data manipulation package in Python.

Python Pandas

Pandas is an open-source Python library for data analysis. It’s a fast, powerful, flexible, and easy-to-use open-source library for Python. It provides ready-to-use high-performance data structures and data analysis tools.

You can also checkout other python tutorials:

CSV File Format

CSV (comma-separated values) is the most common file format for storing plain text data. Widely used for data exchange between servers, CSV files separate each data value with a comma.

Python DataFrame

A DataFrame, a two-dimensional, size-mutable, and heterogeneous tabular data structure, can store data of different data types.

How to Read CSV file in Python Pandas

The read_csv() function in the Pandas library is a method for reading CSV (Comma-Separated Values) files into a Pandas DataFrame.

Syntax of Pandas read_csv()

pd.read_csv(filepath_or_buffer, sep=' ,' , header='infer',  index_col=None, usecols=None, engine=None, skiprows=None, nrows=None)

The read_csv() function has number of parameters out of which one is mandatory and others are optional. This mandatory parameter specifies the CSV file we want to read.

The Parameters are:

  • filepath: Path of CSV file which needs to be read.
  • sep: data separator, default is a comma.
  • header: An integer or a list of integers that represents the row numbers to be used as column names.
  • usecols: Specify only selected columns to be displayed in the output.
  • skiprows: Specify the rows that are to be skipped in the output.
  • nrows: number of rows to be displayed.
# Import pandas
import pandas as pd
# reading csv file
df = pd.read_csv("sample.csv")

In the above code, we have done following things:

Step 1: Import pandas using import.
Step 2: Read CSV file using the read_csv() file.

The read_csv() function reads the contents of the sample.csv file into a Pandas DataFrame named df.

We can read CSV files using the relative path as well as from URL path. You can also choose what columns are needed to export from CSV file.

Show the first 5 rows of the DataFrame

The head() method returns 5 rows from dataframe.

print(df.head())

Modify Delimeter

You can specify the delimiter parameter to change the separator character from the default comma to another character.

df = pd.read_csv("example.csv", delimiter=";")

in the above code, we have specified semicolon character should be used as the separator in the CSV file.

Conclusion

The tutorial covered how to read CSV files using the pandas library in Python. Pandas, being a powerful and flexible open-source library for data analysis, has the read_csv() method to import CSV data into a Pandas DataFrame.

We have covered the following key points:

  1. Pandas: which is like a handy tool in Python for playing with data.
  2. CSV files: These are like simple text files to keep data organized.
  3. DataFrames: It’s a fancy term for tables that help pandas manage data neatly.
  4. read_csv(): This method in pandas to read data from CSV files, with some extra options you can choose.
  5. Practical Examples: Read a sample data file, and displayed the first 5 rows of our table using the head() method.

Leave a Reply

Your email address will not be published. Required fields are marked *