Thursday, September 21, 2023
HomeProgrammingPythonHow to read CSV in Python

How to read CSV in Python

This post was last Updated on by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.

Unlock the power of data manipulation with Python as we dive into the realm of reading CSV files. CSV (Comma-Separated Values) files are a popular way to store and exchange tabular data. In this guide, you’ll embark on a journey to learn the art of reading CSV files using Python’s built-in csv module.

Discover the step-by-step process of opening, parsing, and harnessing the data within CSV files. Whether you’re a beginner or a seasoned programmer, this tutorial will equip you with the skills to effortlessly extract valuable insights from your data, making your Python coding experience even more versatile and impactful.

Also ReadHow to Print All Files in a Directory Using Python [5 Methods]

How to read CSV in Python

CSV stands for Comma Separated Value and stores a collection of comma-separated values, and the file extension is .csv.

There are two ways to read a CSV file in Python.

They are:

  • Using pandas library
  • Using CSV library

Also ReadPython List Methods With Syntax and Examples

Using pandas library

The read_csv() is a method in the pandas library used to read the data in the CSV file. Before using the read_csv() method, import the pandas package and use the read_csv method.

Below is the syntax:

import pandas as pd
data = pd.read_csv( ‘filename’ , delimiter = ‘delimiter_symbol’ )

The read_csv() method accepts two arguments – filename and delimiter. If the file is in a different location/folder with consideration to the source code, then we also need to specify the path of the file along with the filename.

Below is a simple example program that uses a fish.csv file on the below Kaggle website.

Code:

# import required libraries
import pandas as pd


# load dataset
data = pd.read_csv('Fish.csv' , delimiter = ',')


# print data
print(data)

Output:

Species  Weight  Length1  Length2  Length3   Height   Width
0     Bream   242.0     23.2     25.4     30.0  11.5200  4.0200
1     Bream   290.0     24.0     26.3     31.2  12.4800  4.3056
2     Bream   340.0     23.9     26.5     31.1  12.3778  4.6961
3     Bream   363.0     26.3     29.0     33.5  12.7300  4.4555
4     Bream   430.0     26.5     29.0     34.0  12.4440  5.1340
..      ...     ...      ...      ...      ...      ...     ...
154   Smelt    12.2     11.5     12.2     13.4   2.0904  1.3936
155   Smelt    13.4     11.7     12.4     13.5   2.4300  1.2690
156   Smelt    12.2     12.1     13.0     13.8   2.2770  1.2558
157   Smelt    19.7     13.2     14.3     15.2   2.8728  2.0672
158   Smelt    19.9     13.8     15.0     16.2   2.9322  1.8792

[159 rows x 7 columns]

Also ReadHow to Get User Input in Python

Using CSV library

In the CSV module, The reader() method allows us to read the data from the CSV file, which is opened using the open() method.

The open() method accepts two parameters i.e., file name and mode of the file that needs to be opened i.e., read mode, write mode, etc. By default, the file opened will be in read mode if the mode is not specified.

Below is a simple example program that uses the CSV module to load and read the fish.csv file.

Code:

# import required libraries
import csv


# open the csv file in read mode
with open("Fish.csv", 'r') as file:


  # read the data in the file
  data = csv.reader(file)


  # get data line by line
  for line in data:
    print(line)

Output

['\ufeffSpecies', 'Weight', 'Length1', 'Length2', 'Length3', 'Height', 'Width']
['Bream', '242', '23.2', '25.4', '30', '11.52', '4.02']
['Bream', '290', '24', '26.3', '31.2', '12.48', '4.3056']
['Bream', '340', '23.9', '26.5', '31.1', '12.3778', '4.6961']
['Bream', '363', '26.3', '29', '33.5', '12.73', '4.4555']
['Bream', '430', '26.5', '29', '34', '12.444', '5.134']
['Bream', '450', '26.8', '29.7', '34.7', '13.6024', '4.9274']
['Bream', '500', '26.8', '29.7', '34.5', '14.1795', '5.2785']
['Bream', '390', '27.6', '30', '35', '12.67', '4.69']
['Bream', '450', '27.6', '30', '35.1', '14.0049', '4.8438']
['Bream', '500', '28.5', '30.7', '36.2', '14.2266', '4.9594']
['Bream', '475', '28.4', '31', '36.2', '14.2628', '5.1042']
['Bream', '500', '28.7', '31', '36.2', '14.3714', '4.8146']

Explanation: Here, we opened the Fish.csv file in read mode using the open() method and read the data from the file using the reader() method imported from the CSV module.

Also ReadPython filenotfounderror — How to fix filenotfounderror in Python

Conclusion

Learn how to read CSV files using Python’s csv module. Step-by-step guide for beginners and experienced programmers. Effortlessly extract and manipulate data from CSV files for insightful analysis. Boost your data manipulation skills with this comprehensive tutorial.

Other related Python tutorials:

Himanshu Tyagi
Himanshu Tyagihttps://www.codeitbro.com/author/admin/
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. Founder Cool SaaS Hunter. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
RELATED ARTICLES

Most Popular

Recent Posts

- Advertisment -