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:
- Reading Excel Using Python Pandas
- Read and Write CSV Data Using Python
- Import CSV File into MongoDB using Python
- Read CSV File Using read_csv() method
- Read CSV file Using Numpy
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:
- Pandas: which is like a handy tool in Python for playing with data.
- CSV files: These are like simple text files to keep data organized.
- DataFrames: It’s a fancy term for tables that help pandas manage data neatly.
read_csv()
: This method in pandas to read data from CSV files, with some extra options you can choose.- Practical Examples: Read a sample data file, and displayed the first 5 rows of our table using the
head()
method.