Reading Stock Data from a CSV File with Python Pandas
Now that you learned how easy it is to work with spreadsheet data in Pandas, let’s practice using some real financial data! In this activity, you will create a DataFrame from a CSV file and then explore its contents using the DataFrame’s built-in functions.

Starter file
1. Import the pandas library as pd and the Path class from pathlib
# initial imports
2. Create a DataFrame by reading in a CSV file
# set the file path # create a Pandas DataFrame from a csv file
3. Explore the data
# get the first 10 rows from the DataFrame
4. Fix the column names
# set column names # recreate the DataFrame # add column names
5. Get the first 10 rows
# get the first 10 rows from the DataFrame
6. Challenge: Get the bottom 10 rows
# get the bottom 10 rows from the DataFrame
Instructions
Using the starter file, complete the following steps.
- Import the Pandas library by initializing the program with
import pandas as pd. - Create a DataFrame by reading in the
shopify_stock_data.csvfile containing historical price data for Shopify from 2015 to 2019 at the Toronto Stock Exchange. - Perform an initial data exploration by getting the top
10rows of the DataFrame. - Oh no! There are no column names on the DataFrame. Fix this problem by recreating the DataFrame and setting the column names to “Date”, “Close”, “Volume”, “Open”, “High”, “Low”.
- When the column names are fixed, get the first
10rows from the DataFrame.
Challenge
Get the bottom 10 rows of the DataFrame. Use Google to figure out how to do this.
Hint
Consult the Pandas head() function documentation.
Solution
1. Import the pandas library as pd and the Path class from pathlib
In [1]:
# initial imports import pandas as pd from pathlib import Path
2. Create a DataFrame by reading in a CSV file
In [2]:
# set the file path
file_path = Path("../Resources/shopify_stock_data.csv")
# create a Pandas DataFrame from a csv file
df = pd.read_csv(file_path)
3. Explore the data
In [3]:
# get the first 10 rows from the DataFrame df.head(10)
Out [3]:
“\n”, “| \n”, ” | 5/21/2015 16:00:00 | \n”, ”31.25 | \n”, ”211058 | \n”, ”35.03 | \n”, ”35.03.1 | \n”, ”30 | \n”, ”
|---|---|---|---|---|---|---|
| 0 | \n”, ”5/22/2015 16:00:00 | \n”, ”34.94 | \n”, ”224174 | \n”, ”32.32 | \n”, ”38.00 | \n”, ”32.00 | \n”, ”
| 1 | \n”, ”5/25/2015 16:00:00 | \n”, ”37.26 | \n”, ”105460 | \n”, ”35.00 | \n”, ”37.47 | \n”, ”35.00 | \n”, ”
| 2 | \n”, ”5/26/2015 16:00:00 | \n”, ”36.92 | \n”, ”75935 | \n”, ”37.26 | \n”, ”37.69 | \n”, ”36.30 | \n”, ”
| 3 | \n”, ”5/27/2015 16:00:00 | \n”, ”34.50 | \n”, ”135778 | \n”, ”38.00 | \n”, ”38.16 | \n”, ”33.63 | \n”, ”
| 4 | \n”, ”5/28/2015 16:00:00 | \n”, ”34.00 | \n”, ”28756 | \n”, ”34.60 | \n”, ”34.60 | \n”, ”33.14 | \n”, ”
| 5 | \n”, ”5/29/2015 16:00:00 | \n”, ”33.52 | \n”, ”15319 | \n”, ”33.71 | \n”, ”34.11 | \n”, ”33.35 | \n”, ”
| 6 | \n”, ”6/1/2015 16:00:00 | \n”, ”33.85 | \n”, ”8839 | \n”, ”33.73 | \n”, ”34.27 | \n”, ”33.52 | \n”, ”
| 7 | \n”, ”6/2/2015 16:00:00 | \n”, ”33.13 | \n”, ”7318 | \n”, ”34.72 | \n”, ”34.72 | \n”, ”33.13 | \n”, ”
| 8 | \n”, ”6/3/2015 16:00:00 | \n”, ”34.19 | \n”, ”29821 | \n”, ”33.98 | \n”, ”34.26 | \n”, ”33.00 | \n”, ”
| 9 | \n”, ”6/4/2015 16:00:00 | \n”, ”33.45 | \n”, ”32221 | \n”, ”34.99 | \n”, ”34.99 | \n”, ”31.88 | \n”, ”
4. Fix the column names
In [4]:
# set column names col_names = ["date", "close", "volume", "open", "high", "low"] # recreate the DataFrame df = pd.read_csv(file_path, header = None) # add column names df.columns = col_names
5. Get the first 10 rows
In [5]:
# get the first 10 rows from the DataFrame df.head(10)
Out [5]:
“| \n”, ” | date | \n”, ”close | \n”, ”volume | \n”, ”open | \n”, ”high | \n”, ”low | \n”, ”
|---|---|---|---|---|---|---|
| 0 | \n”, ”5/21/2015 16:00:00 | \n”, ”31.25 | \n”, ”211058 | \n”, ”35.03 | \n”, ”35.03 | \n”, ”30.00 | \n”, ”
| 1 | \n”, ”5/22/2015 16:00:00 | \n”, ”34.94 | \n”, ”224174 | \n”, ”32.32 | \n”, ”38.00 | \n”, ”32.00 | \n”, ”
| 2 | \n”, ”5/25/2015 16:00:00 | \n”, ”37.26 | \n”, ”105460 | \n”, ”35.00 | \n”, ”37.47 | \n”, ”35.00 | \n”, ”
| 3 | \n”, ”5/26/2015 16:00:00 | \n”, ”36.92 | \n”, ”75935 | \n”, ”37.26 | \n”, ”37.69 | \n”, ”36.30 | \n”, ”
| 4 | \n”, ”5/27/2015 16:00:00 | \n”, ”34.50 | \n”, ”135778 | \n”, ”38.00 | \n”, ”38.16 | \n”, ”33.63 | \n”, ”
| 5 | \n”, ”5/28/2015 16:00:00 | \n”, ”34.00 | \n”, ”28756 | \n”, ”34.60 | \n”, ”34.60 | \n”, ”33.14 | \n”, ”
| 6 | \n”, ”5/29/2015 16:00:00 | \n”, ”33.52 | \n”, ”15319 | \n”, ”33.71 | \n”, ”34.11 | \n”, ”33.35 | \n”, ”
| 7 | \n”, ”6/1/2015 16:00:00 | \n”, ”33.85 | \n”, ”8839 | \n”, ”33.73 | \n”, ”34.27 | \n”, ”33.52 | \n”, ”
| 8 | \n”, ”6/2/2015 16:00:00 | \n”, ”33.13 | \n”, ”7318 | \n”, ”34.72 | \n”, ”34.72 | \n”, ”33.13 | \n”, ”
| 9 | \n”, ”6/3/2015 16:00:00 | \n”, ”34.19 | \n”, ”29821 | \n”, ”33.98 | \n”, ”34.26 | \n”, ”33.00 | \n”, ”
6. Challenge: Get the bottom 10 rows
In [6]:
# get the bottom 10 rows from the DataFrame df.tail(10)
Out [6]:
“| \n”, ” | date | \n”, ”close | \n”, ”volume | \n”, ”open | \n”, ”high | \n”, ”low | \n”, ”
|---|---|---|---|---|---|---|
| 1145 | \n”, ”12/13/2019 16:00:00 | \n”, ”508.43 | \n”, ”312817 | \n”, ”488.94 | \n”, ”516.60 | \n”, ”488.90 | \n”, ”
| 1146 | \n”, ”12/16/2019 16:00:00 | \n”, ”517.42 | \n”, ”221975 | \n”, ”513.98 | \n”, ”521.26 | \n”, ”502.85 | \n”, ”
| 1147 | \n”, ”12/17/2019 16:00:00 | \n”, ”510.85 | \n”, ”252381 | \n”, ”517.20 | \n”, ”521.97 | \n”, ”500.77 | \n”, ”
| 1148 | \n”, ”12/18/2019 16:00:00 | \n”, ”520.56 | \n”, ”391152 | \n”, ”513.00 | \n”, ”530.17 | \n”, ”512.97 | \n”, ”
| 1149 | \n”, ”12/19/2019 16:00:00 | \n”, ”516.06 | \n”, ”178895 | \n”, ”520.99 | \n”, ”526.66 | \n”, ”513.85 | \n”, ”
| 1150 | \n”, ”12/20/2019 16:00:00 | \n”, ”513.22 | \n”, ”715483 | \n”, ”516.20 | \n”, ”524.99 | \n”, ”510.29 | \n”, ”
| 1151 | \n”, ”12/23/2019 16:00:00 | \n”, ”511.62 | \n”, ”243940 | \n”, ”516.83 | \n”, ”524.99 | \n”, ”510.37 | \n”, ”
| 1152 | \n”, ”12/24/2019 13:30:00 | \n”, ”525.39 | \n”, ”125214 | \n”, ”512.46 | \n”, ”527.17 | \n”, ”508.21 | \n”, ”
| 1153 | \n”, ”12/27/2019 16:00:00 | \n”, ”534.76 | \n”, ”156355 | \n”, ”539.97 | \n”, ”544.00 | \n”, ”528.00 | \n”, ”
| 1154 | \n”, ”12/30/2019 16:00:00 | \n”, ”517.79 | \n”, ”162031 | \n”, ”535.36 | \n”, ”535.36 | \n”, ”512.57 | \n”, ”