Explore the oldest creature in starwars

Starwars dataset

The starwars dataset contains the characters and other information in the films. The data is stored in json schema. You can download these files by requests library in python. To download data from rest APIs, you need to append the base url and endpoint paths. In this case, the base url is https://swapi.dev/api.

import requests
import os
base_url = 'https://swapi.dev/api'
urls = [f'people/{x+1}' for x in range(82)]
data = []
for url in urls:
    resp = requests.get(os.path.join(base_url,url))
    data.append(resp.json())

The oldest person in starwars

Now all the information regarding characters in starwars are stored in a list named after data. First, let's transform the list into a pandas dataframe.

df = pd.json_normalize(data)

The birth year of characters in starwars is in the form of XXX(numeric year) + BBY/ABY. BBY is the abbreviation of Before Battle of Yavin and ABY is the abbreviation of After Battle of Yavin. Thus, if we want to find the oldest person, we need to find the largest number + BBY.

df["year"] = df.birth_year.str.extract('(^[0-9.]+)')
df["year"] = df.year.astype("float")
df["BBY"] = df.birth_year.str.extract('(BBY$)')
df.iloc[df[df.BBY == 'BBY'].year.idxmax(),:]

Here, the numeric part of birth year is extracted from the string and the largest number within people born before Battle of Yavin is found. The oldest person is Yoda. Our next goal is to find out which films this character is involved.

Get the nested API

As we can see from the output above, the films are nested in the form of urls. We need to extract the nested urls to figure out the titles of the films. The following function downloads all the nested APIs regarding films:

def get_nested(d):
    urls = d['films']
    films = [requests.get(url).json() for url in urls]
    d['films']  = films
    return d
data_nested = get_nested(data[19])
[x['title'] for x in data_nested['films']]

Yoda shows up in four films in the series of starwars.

References

[1] https://swapi.dev/documentation

links

social