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.

  1. Import the Pandas library by initializing the program with import pandas as pd.
  2. Create a DataFrame by reading in the shopify_stock_data.csv file containing historical price data for Shopify from 2015 to 2019 at the Toronto Stock Exchange.
  3. Perform an initial data exploration by getting the top 10 rows of the DataFrame.
  4. 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”.
  5. When the column names are fixed, get the first 10 rows 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”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, “
5/21/2015 16:00:0031.2521105835.0335.03.130
05/22/2015 16:00:0034.9422417432.3238.0032.00
15/25/2015 16:00:0037.2610546035.0037.4735.00
25/26/2015 16:00:0036.927593537.2637.6936.30
35/27/2015 16:00:0034.5013577838.0038.1633.63
45/28/2015 16:00:0034.002875634.6034.6033.14
55/29/2015 16:00:0033.521531933.7134.1133.35
66/1/2015 16:00:0033.85883933.7334.2733.52
76/2/2015 16:00:0033.13731834.7234.7233.13
86/3/2015 16:00:0034.192982133.9834.2633.00
96/4/2015 16:00:0033.453222134.9934.9931.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”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, “
dateclosevolumeopenhighlow
05/21/2015 16:00:0031.2521105835.0335.0330.00
15/22/2015 16:00:0034.9422417432.3238.0032.00
25/25/2015 16:00:0037.2610546035.0037.4735.00
35/26/2015 16:00:0036.927593537.2637.6936.30
45/27/2015 16:00:0034.5013577838.0038.1633.63
55/28/2015 16:00:0034.002875634.6034.6033.14
65/29/2015 16:00:0033.521531933.7134.1133.35
76/1/2015 16:00:0033.85883933.7334.2733.52
86/2/2015 16:00:0033.13731834.7234.7233.13
96/3/2015 16:00:0034.192982133.9834.2633.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”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, “
dateclosevolumeopenhighlow
114512/13/2019 16:00:00508.43312817488.94516.60488.90
114612/16/2019 16:00:00517.42221975513.98521.26502.85
114712/17/2019 16:00:00510.85252381517.20521.97500.77
114812/18/2019 16:00:00520.56391152513.00530.17512.97
114912/19/2019 16:00:00516.06178895520.99526.66513.85
115012/20/2019 16:00:00513.22715483516.20524.99510.29
115112/23/2019 16:00:00511.62243940516.83524.99510.37
115212/24/2019 13:30:00525.39125214512.46527.17508.21
115312/27/2019 16:00:00534.76156355539.97544.00528.00
115412/30/2019 16:00:00517.79162031535.36535.36512.57
\n”,

We will be happy to hear your thoughts

Leave a reply