How to import a CSV file in Python?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

0

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

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

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

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.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.