Import CSV File in Python

This tutorial explains how to read a CSV file in python. It outlines many examples of loading a CSV file into Python.

Load Package

You have to load required package i.e. pandas
import pandas as pd
Create Dummy Data for Import

The program below creates a sample data frame which can be used further for demonstration.
dt = {'ID': [11, 12, 13, 14, 15],
            'first_name': ['David', 'Jamie', 'Steve', 'Stevart', 'John'],
            'company': ['Aon', 'TCS', 'Google', 'RBS', '.'],
            'salary': [74, 76, 96, 71, 78]}
mydt = pd.DataFrame(dt, columns = ['ID', 'first_name', 'company', 'salary'])
Sample Data
Save data as csv in the working directory
mydt.to_csv('workingfile.csv', index=False)
Example 1 : Read CSV file with header row
mydata  = pd.read_csv("workingfile.csv")
Example 2 : Read CSV file without header row
mydata0  = pd.read_csv("workingfile.csv", header = None)
If you specify "header = None", python would assign a series of numbers starting from 0 to (number of columns - 1). See the output shown below -
Output
Example 3 : Specifying missing values
mydata00  = pd.read_csv("workingfile.csv", na_values=['.'])
Example 4 : Setting Index Column to ID
mydata01  = pd.read_csv("workingfile.csv", index_col ='ID')
Python : Setting Index Column

Example 5 : Read CSV File from URL
mydata02  = pd.read_csv("http://winterolympicsmedals.com/medals.csv")

Example 6 : Skip First 5 Rows While Importing CSV
mydata03  = pd.read_csv("http://winterolympicsmedals.com/medals.csv", skiprows=5)
It reads data from 6th row (6th row would be a header row)

Example 7 : Skip Last 5 Rows While Importing CSV
mydata04  = pd.read_csv("http://winterolympicsmedals.com/medals.csv", skip_footer=5)
It excludes last5 rows.

Example 8 : Read only first 5 rows
mydata05  = pd.read_csv("http://winterolympicsmedals.com/medals.csv", nrows=5)
Example 9 : Interpreting "," as thousands separator
mydata06 = pd.read_csv("http://winterolympicsmedals.com/medals.csv", thousands=",")
Example 10 : Read only specific columns
mydata07 = pd.read_csv("http://winterolympicsmedals.com/medals.csv", usecols=(1,5,7))
The above code reads only columns placed at first, fifth and seventh position. 
Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

0 Response to "Import CSV File in Python"

Post a Comment

Next → ← Prev