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
In [1]:
## How to import a CSV file in Python
def Snippet_171():
    print()
    print(format('How to import a CSV file in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import csv
    import numpy
    import pandas

    # Load CSV (using python)
    filename = 'pima.indians.diabetes.data.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)

    # Load CSV (using numpy)
    filename = 'pima.indians.diabetes.data.csv'
    raw_data = open(filename, 'rt')
    data = numpy.loadtxt(raw_data, delimiter=",")
    print(data.shape)

    # Load CSV (using Pandas)
    filename = 'pima.indians.diabetes.data.csv'
    names = ['preg', 'plas', 'pres', 'skin', 'test',
             'mass', 'pedi', 'age', 'class']
    data = pandas.read_csv(filename, names=names)
    print(data.shape)

Snippet_171()
************************How to import a CSV file in Python************************
(768, 9)
(768, 9)
(768, 9)

Relevant Projects

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.

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.

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.

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.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

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.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

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.