<jupyter_start><jupyter_text>Learning Objectives* How to extract data from an external API.* How to read the documentation of an API.* Inspecting and cleaning the collected data.* Lastly, saving the data to a file using pandas.<jupyter_code>import requests
import pandas as pd
r = requests.get("https://api.themoviedb.org/3/genre/movie/list?api_key=<<ADD YOUR API KEY>>&language=en-US")


r
r.text
data = r.json()
type(data)
df = pd.DataFrame(data)

df
r.json() # data
data['genres'][0]['id']
id_list = []
genres = []

for genre in data['genres']:
    id_list.append(genre['id'])
    genres.append(genre['name'])
id_list
genres
df = pd.DataFrame({'id': id_list, 'genre': genres})
df
df.to_csv("./movies_genres_data.csv", index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15203145478374835, 'fraction_numerical': 0.002621231979030144, 'mean_word_length': 5.47457627118644, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '430747', 'license': 'MIT', 'n_tokens_mistral': 284, 'n_tokens_neox': 275, 'n_words': 72, 'path': 'module_2_programming/data_collection_from_api.ipynb', 'repo_name': 'wiplane/foundations-of-datascience-ml', 'size': '763'}