How to import a CSV file in Python?

How to import a CSV file in Python?

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)

Relevant Projects

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.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.