Load a CSV File With Python and Pandas

Resources
Right-click -> Save as…

Prerequisites

  • Install the Pandas library for your Python environment
  • Cells in this notebook expect the Car Sales.csv file to be in certain locations; specifics are in the cell itself
  • Resources to help you practice

First Things First

import pandas as pd

Load Data From a CSV File

File is in the same directory as your Jupyter Notebook

# Read the CSV file
car_sales_data = pd.read_csv("Car Sales.csv")

# Show the first 5 rows
car_sales_data.head(5)
DealershipName RedCars SilverCars BlackCars BlueCars MonthSold YearSold
0 Clyde's Clunkers 902.0 650.0 754.0 792.0 1.0 2018.0
1 Clyde's Clunkers 710.0 476.0 518.0 492.0 2.0 2018.0
2 Clyde's Clunkers 248.0 912.0 606.0 350.0 3.0 2018.0
3 Clyde's Clunkers 782.0 912.0 858.0 446.0 4.0 2018.0
4 Clyde's Clunkers 278.0 354.0 482.0 752.0 5.0 2018.0

File is in a different directory than your Jupyter Notebook

The example will use your “home directory” to make this example applicable across operating systems, but you can use any path as long as the file exists there…

from os.path import expanduser as ospath

user_home_directory = ospath("~")
# Make sure to use "/" slashes and not "\" slashes
# There actually needs to be folders named "Path" and "To" and "CSV" and "File"
# in your home directory (the "~" means "home directory") for this cell to work
csv_file_path = user_home_directory + "/Path/To/CSV/File/Car Sales.csv"

other_path_car_sales_data = pd.read_csv(csv_file_path)

# Show the first 5 rows
other_path_car_sales_data.head(5)
DealershipName RedCars SilverCars BlackCars BlueCars MonthSold YearSold
0 Clyde's Clunkers 902.0 650.0 754.0 792.0 1.0 2018.0
1 Clyde's Clunkers 710.0 476.0 518.0 492.0 2.0 2018.0
2 Clyde's Clunkers 248.0 912.0 606.0 350.0 3.0 2018.0
3 Clyde's Clunkers 782.0 912.0 858.0 446.0 4.0 2018.0
4 Clyde's Clunkers 278.0 354.0 482.0 752.0 5.0 2018.0

From a URL

# Note the URL Encoding with "%20" for spaces
url_to_csv_file = "https://github.com/andrewcbancroft/datadaylife-blog/raw/master/datasets/Car%20Sales.csv"

# Read the CSV file
url_car_sales_data = pd.read_csv(url_to_csv_file)

# Show the first 5 rows
url_car_sales_data.head(5)
DealershipName RedCars SilverCars BlackCars BlueCars MonthSold YearSold
0 Clyde's Clunkers 902.0 650.0 754.0 792.0 1.0 2018.0
1 Clyde's Clunkers 710.0 476.0 518.0 492.0 2.0 2018.0
2 Clyde's Clunkers 248.0 912.0 606.0 350.0 3.0 2018.0
3 Clyde's Clunkers 782.0 912.0 858.0 446.0 4.0 2018.0
4 Clyde's Clunkers 278.0 354.0 482.0 752.0 5.0 2018.0