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

1import pandas as pd

Load Data From a CSV File

File is in the same directory as your Jupyter Notebook

1# Read the CSV file
2car_sales_data = pd.read_csv("Car Sales.csv")
3
4# Show the first 5 rows
5car_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…

1from os.path import expanduser as ospath
2
3user_home_directory = ospath("~")
1# Make sure to use "/" slashes and not "\" slashes
2# There actually needs to be folders named "Path" and "To" and "CSV" and "File"
3# in your home directory (the "~" means "home directory") for this cell to work
4csv_file_path = user_home_directory + "/Path/To/CSV/File/Car Sales.csv"
5
6other_path_car_sales_data = pd.read_csv(csv_file_path)
7
8# Show the first 5 rows
9other_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

1# Note the URL Encoding with "%20" for spaces
2url_to_csv_file = "/datasets/Car%20Sales.csv"
3
4# Read the CSV file
5url_car_sales_data = pd.read_csv(url_to_csv_file)
6
7# Show the first 5 rows
8url_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