The CSV (Comma Separated Values) format is quite popular for storing data. A large number of datasets are present as CSV files which can be used either directly in a spreadsheet software like Excel or can be loaded up in programming languages like R or Python. Pandas dataframes are quite powerful for handling two-dimensional tabular data. In this tutorial, we’ll look at how to read a csv file as a pandas dataframe in python.
How to read csv files in python using pandas?
The pandas read_csv()
function is used to read a CSV file into a dataframe. It comes with a number of different parameters to customize how you’d like to read the file. The following is the general syntax for loading a csv file to a dataframe:
import pandas as pd
df = pd.read_csv(path_to_file)
Here, path_to_file
is the path to the CSV file you want to load. It can be any valid string path or a URL (see the examples below). It returns a pandas dataframe. Let’s look at some of the different use-cases of the read_csv()
function through examples –
Examples
Before we proceed, let’s get a sample CSV file that we’d be using throughout this tutorial. We’ll be using the Iris dataset which you can download from Kaggle. Here’s a snapshot of how it looks when opened in excel:
1. Read CSV from its location on your machine
To read a CSV file locally stored on your machine pass the path to the file to the read_csv()
function. You can pass a relative path, that is, the path with respect to your current working directory or you can pass an absolute path.
# read csv using relative path
import pandas as pd
df = pd.read_csv('Iris.csv')
print(df.head())
Output:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 1 5.1 3.5 1.4 0.2 Iris-setosa
1 2 4.9 3.0 1.4 0.2 Iris-setosa
2 3 4.7 3.2 1.3 0.2 Iris-setosa
3 4 4.6 3.1 1.5 0.2 Iris-setosa
4 5 5.0 3.6 1.4 0.2 Iris-setosa
In the above example, the CSV file Iris.csv
is loaded from its location using a relative path. Here, the file is present in the current working directory. You can also read a CSV file from its absolute path. See the example below:
# read csv using absolute path
import pandas as pd
df = pd.read_csv(r"C:\Users\piyush\Downloads\Iris.csv")
print(df.head())
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 1 5.1 3.5 1.4 0.2 Iris-setosa
1 2 4.9 3.0 1.4 0.2 Iris-setosa
2 3 4.7 3.2 1.3 0.2 Iris-setosa
3 4 4.6 3.1 1.5 0.2 Iris-setosa
4 5 5.0 3.6 1.4 0.2 Iris-setosa
Here, the same CSV file is read from its absolute path.
2. Read CSV from a URL
You can also read a CSV file from its URL. Pass the URL to the read_csv()
function and it’ll read the corresponding file to a dataframe. The Iris dataset can also be downloaded from the UCI Machine Learning Repository. Let’s use their dataset download URL to read it as a dataframe.
import pandas as pd
df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")
df.head()
Output:
5.1 3.5 1.4 0.2 Iris-setosa
0 4.9 3.0 1.4 0.2 Iris-setosa
1 4.7 3.2 1.3 0.2 Iris-setosa
2 4.6 3.1 1.5 0.2 Iris-setosa
3 5.0 3.6 1.4 0.2 Iris-setosa
4 5.4 3.9 1.7 0.4 Iris-setosa
You can see that the read_csv()
function is able to read a dataset from its URL. It is interesting to note that in this particular data source, we do not have headers. The read_csv()
function infers the header by default and here uses the first row of the dataset as the header.
3. Read a CSV file without a header
In the above example, you saw that if the dataset does not have a header, the read_csv()
function infers it by itself and uses the first row of the dataset as the header. You can change this behavior through the header
parameter, pass None
if your dataset does not have a header. You can also pass a custom list of integers as a header.
import pandas as pd
df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", header=None)
df.head()
Output:
0 1 2 3 4
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
In the above example, we pass header=None
to the read_csv()
function since the dataset did not have a header.
4. Read a CSV file and give custom column names
You can give custom column names to your dataframe when reading a CSV file using the read_csv()
function. Pass your custom column names as a list to the names
parameter.
import pandas as pd
df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
names = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species'])
print(df.head())
Output:
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
5. Read CSV with a column as index
You can also use a column as the row labels of the dataframe. Pass the column name to the index_col
parameter. Going back to the Iris.csv
we downloaded from Kaggle. Here, we use the Id
columns as the dataframe index.
# read csv with a column as index
import pandas as pd
df = pd.read_csv('Iris.csv', index_col='Id')
print(df.head())
Output:
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
Id
1 5.1 3.5 1.4 0.2 Iris-setosa
2 4.9 3.0 1.4 0.2 Iris-setosa
3 4.7 3.2 1.3 0.2 Iris-setosa
4 4.6 3.1 1.5 0.2 Iris-setosa
5 5.0 3.6 1.4 0.2 Iris-setosa
In the above example, you can see that the Id
column is used as the row index of the dataframe df
. You can also pass multiple columns as list to the index_col
parameter to be used as row index.
6. Read only a subset of columns of a CSV
You can also specify the subset of columns to read from the dataset. Pass the subset of columns you want as a list to the usecols
parameter. For example, let’s read all the columns from Iris.csv
except Id
.
# read csv with a column as index
import pandas as pd
df = pd.read_csv('Iris.csv', usecols=['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species'])
print(df.head())
Output:
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
In the above example, the returned dataframe does not have an Id
column.
7. Read only the first n rows of a CSV
You can also specify the number of rows of a file to read using the nrows
parameter to the read_csv()
function. Particularly useful when you want to read a small segment of a large file.
# read csv with a column as index
import pandas as pd
df = pd.read_csv('Iris.csv', nrows=3)
print(df.head())
Output:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 1 5.1 3.5 1.4 0.2 Iris-setosa
1 2 4.9 3.0 1.4 0.2 Iris-setosa
2 3 4.7 3.2 1.3 0.2 Iris-setosa
In the above example, we read only the first three rows of the file Iris.csv
.
These are just some of the things you can do when reading a CSV file to dataframe. Pandas dataframes also provide a number of useful features to manipulate the data once the dataframe has been created.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
More on Pandas DataFrames –
- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame