Skip to main content
Noble Desktop/2 min read

API's with Python

Python Workflow

1

Set Up venv

python -m venv .venv — isolated environment per project.

2

Install Dependencies

pip install -r requirements.txt — pin versions for reproducibility.

3

Write & Test

Write functions; test with pytest as you go.

4

Run & Deploy

python script.py locally, deploy to your platform of choice.

Master Python at Noble Desktop

Noble Desktop's Python Programming Immersive covers AI APIs, data analysis, and modern Python development.

In this video, we're going to look at how to manage data using API's in Python.

Video Transcription

Hi, my name is Art, and in this video I'm going to show you how to read data from APIs and convert that data into a Python data structure such as a Pandas DataFrame.

To use Pandas, you need to import it: `import pandas as pd`.

You also need to import the Requests Library, which helps Python to get online and send requests to a server for data. Most APIs require registration and sometimes require a credit card. To avoid this, we can use the Rick and Morty API which is free.

We can use the `get` method from the Requests Library to send a request to a server and get data. We will assign this data to a variable called `raw`, since it's a response. We can then parse this data, which most likely will come in the form of JSON (JavaScript Object Notation). JSON looks like a dictionary and we can use `.keys()` to get the options.

We can then grab the values we want such as `name`, `status`, and `species`, and use dictionary notation to get them. Now we can initialize the DataFrame and use `pd.json_normalize()` to convert the data into a DataFrame. We can assign it to a variable called `df` and then do something else with it such as writing it into a text file or a SQL database.

Video Transcript9 sections

1Full Video Transcript

Hi, my name is Art. I teach Python. In this video, I'm going to show you how you could read data from APIs and how you could convert that data into some other Python data structure, such as maybe a Pandas DataFrame.

So what we're going to do: if you want to use Pandas, you need to import Pandas as PD. That's the first thing. Then we need to import another library: import requests. So what is requests? Requests is a small Python library, very popular.

2Understanding APIs and HTTP Requests

You have to understand what is an API. An API is some data served from a server. So you need to get online. Python cannot get online because Python is a programming language, so Python requires some help such as requests to literally get online and send requests to a server for data.

Now, most APIs require a lot of registration, and sometimes you need to provide credit card information. I don't want to do any of that, so here what we're going to be doing: apparently there is a very popular API, totally free, and that's based on a very popular cartoon, Rick and Morty. If you're fans, hey! They provide all these data about characters. So that would be your URL.