How to import a CSV file in Python?

This recipe helps you import a CSV file in Python

Recipe Objective

Before using any model first thing you have to do is to import the dataset. There are many ways to do it.

So this is the recipe on how we can import a CSV file in Python.

Step 1 - Import the library

import csv import numpy import pandas

We have imported numpy, csv and pandas which is needed.

Step 2 - Loading CSV

We are first importing dataset with use of CSV library. Here we need to pass the file name, the quoting in the csv.reader function and the parameter delimiter which signifies that by which charactor the data is seperated. we can also store it in a object and can use the data by calling the object. filename = "load.csv" raw_data = open(filename, "rt") reader = csv.reader(raw_data, delimiter=",", quoting=csv.QUOTE_NONE) x = list(reader) data = numpy.array(x).astype("float") print(data.shape) We can also do this with the help of numpy. For this we have to pass the file name in numpy.loadtxt and we have to also set the delimiter which signifies that by which charactor the data is seperated. filename = "load.csv" raw_data = open(filename, "rt") data = numpy.loadtxt(raw_data, delimiter=",") print(data.shape) We can also do this with the help of pandas. For CSV we need to make a array of names of columns before importing the data then to import we have to pass the file name and the array we have created to the pandas.read_csv function. filename = "load.csv" names = ["preg", "plas", "pres", "skin", "test", "mass", "pedi", "age", "class"] data = pandas.read_csv(filename, names=names) print(data.shape)

Download Materials

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.