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

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.