Load Data with Python

Now we can load the airports data set in Python, and we can display the head of the airports data set, in Python.

import pandas as pd
myDF = pd.read_csv("/anvil/projects/tdm/data/flights/subset/airports.csv")
myDF.head()

Just try this Python code using the seminar kernel (not the seminar-r kernel) and make sure that you can see the first five rows of the airports data frame.

OK, now that we know how to load a data set in Python, load the data from the Olympics data set, about the Athlete Events. This data is located in this file:

/anvil/projects/tdm/data/olympics/athlete_events.csv

You can also send a specific argument (like the number 12) to a Python method. The head method has a parameter called n that specifies the number of rows that appear. By default, n has value 5, but you can change this value, e.g., by using head(12) instead of head(). Please give this a try! There are also some examples on the documentation page:

In Python, we often use the Pandas library for loading DataFrames. Pandas allows us to check some properties of our data frame. For instance, we can use the shape property to see how many rows and columns the airports DataFrame has:

myDF = pd.read_csv("/anvil/projects/tdm/data/flights/subset/airports.csv")
myDF.shape

Notice that Python starts counting from 0 (as opposed to R, which starts counting from 1). So the initial row of the Pandas DataFrame is row 0. In the head of the DataFrame, as we saw in Question 1, we see rows 0, 1, 2, 3, 4.

If you load the tail of the airports DataFrame (which we used as a demonstration example at the start of Question 1), it displays rows 3371, 3372, 3373, 3374, 3375. As indicated by the shape parameter, the airports DataFrame has 3376 rows altogether, so this makes sense. You can check this as follows:

myDF.tail()

We can also select rows of the data frame that meet certain conditions. For instance, we can extract the airports located in New York City as follows:

myDF[(myDF['city'] == 'New York') & (myDF['state'] == 'NY')]

Now that you know how to find specific rows in a data frame, you might try this on your own: Load the Olympics Athlete Events data frame.

Use the data frame to identify the names of the athletes from Jamaica who competed in the Bobsleigh race during the Winter 1988 Olympics.

There is a fictional movie about this event:

and the real story is given here: