Fetching CSV Data

If the data you want to fetch is in CSV format, we can use the pandas package to fetch and process it.

Let’s consider this example "students.csv" file we have hosted on the Internet:

student_id,final_grade
1,76.7
2,85.1
3,50.3
4,89.8
5,97.4
6,75.5
7,87.2
8,88.0
9,93.9
10,92.5

First we note the URL of where the data resides. Then we pass that as a parameter to the read_csv function from the pandas package, to issue an HTTP GET request:

from pandas import read_csv

# the URL of some CSV data we stored online:
request_url = "https://raw.githubusercontent.com/prof-rossetti/python-for-finance/main/docs/data/gradebook.csv"

df = read_csv(request_url)
print(type(df))
df
<class 'pandas.core.frame.DataFrame'>
student_id final_grade
0 1 76.7
1 2 85.1
2 3 50.3
3 4 89.8
4 5 97.4
5 6 75.5
6 7 87.2
7 8 88.0
8 9 93.9
9 10 92.5

The resulting data is a DataFrame object from pandas. We will return to working with dataframes in more detail in the future. But as some foreshadowing, if we wanted to work with the column of grades, we could access them like this:

df["final_grade"]
0    76.7
1    85.1
2    50.3
3    89.8
4    97.4
5    75.5
6    87.2
7    88.0
8    93.9
9    92.5
Name: final_grade, dtype: float64
print(df["final_grade"].mean())
print(df["final_grade"].median())
83.64
87.6