This tutorial explains how to import a CSV file into python. It outlines many examples of loading a CSV file into Python.
Import Module
Save data as csv in the working directory
Example 3 : Specifying missing values
Example 5 : Read CSV File from URL
Example 6 : Skip First 5 Rows While Importing CSV
Example 7 : Skip Last 5 Rows While Importing CSV
Example 8 : Read only first 5 rows
Import Module
import pandas as pdCreate Dummy Data for Import
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 |
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 |
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.