In this video, we will discuss
Application Program Interfaces, API for short.
Specifically, we will discuss what is an API,
API libraries, REST API,
including request and response.
An API lets two pieces of software talk to each other.
For example, you have your program, you have some data,
you have other software components,
you use the API to communicate with
the API via inputs and outputs.
Just like a function,
you don't have to know how the API
works but just as inputs and outputs.
Pandas is actually a set of
software components much of
which are not even written in Python.
You have some data. You have
a set of software components.
We use the pandas API to process
the data by communicating with
the other software components.
Let's clean up the diagram.
When you create a dictionary and then create
a pandas object with a DataFrame constructor,
in API lingo this is an instance.
The data in the dictionary is passed
along to the pandas API.
You then use the DataFrame to communicate with the API.
When you call the method head,
the DataFrame communicates with
the API displaying the first few rows of the DataFrame.
When you call the method mean,
the API will calculate the mean and return the values.
REST APIs are another popular type of API.
They allow you to communicate
through the Internet allowing you
to take advantage of resources like storage,
access more data,
artificial intelligent algorithms and much more.
The RE stands for representational.
The S stands for state.
The T stands for transfer.
In REST APIs, your program is called the client.
The API communicates with
a web service you call through the Internet.
There is a set of rules regarding communication,
input or request, and output or response.
Here are some common terms.
You or your code can be thought of as a client.
The web service is referred to as a resource.
The client finds the service via an endpoint.
We will review this more in the next section.
The client sends requests to
the resource and the response to the client.
HTTP methods are a way of
transmitting data over the internet.
We tell the REST APIs what to do by sending a request.
The request is usually communicated via an HTTP message.
The HTTP message usually contains a JSON file.
This contains instructions for
what operation we would like the service to perform.
This operation is transmitted to
the web service via the Internet.
The service performs the operation.
In the similar manner, the web service returns
a response via an HTTP message,
where the information is usually
returned via JSON file.
This information is transmitted back to the client.
Sports data is always changing.
This is an excellent application of
an API as it can be constantly updated.
We will use the nba_api by Swar Patel.
The API is always being updated
from endpoints at nba.com.
Is simple to use, so you can
focus on the task of collecting data.
In the nba_api, to make
request for a specific team, it's quite simple.
We don't require a JSON file.
All we require is an ID.
This information is stored locally in the API.
We import the module teams.
The method get_teams returns a list of dictionaries,
which have the same keys but
the values depend on the team.
The dictionary key id has
a unique identifier for each team as a value.
To make things easier,
we can convert the dictionary to a table.
First, we create the function one_dict.
To create a dictionary,
we use the common keys for each team as the keys.
The value is a list.
Each element of the list
corresponds to the values for each team.
We then convert the dictionary to a DataFrame.
Each row contains the information for a different team.
We'll use the teams nickname to find the unique ID.
We can find the row that
contains the warriors as follows.
The ID is the first column.
We can use the following line of code to
access the first column of the DataFrame.
We now have an integer that can be used to
request the warriors information.
The function Leaguegamefinder, will make an API call.
The parameter team_id_nullable is
the unique id for the warriors.
Under the hood, the nba_api is making it an HTTP request.
This is transmitted to nba.com.
The information requested is provided and
is transmitted via an HTTP response.
This is assigned to the object gamefinder.
The gamefinder object has
a method get_data_frame that returns a data frame.
If we view the DataFrame,
we can see it contains information
about all the games the warriors played.
The plus_minus column contains information on the score.
If the value is negative,
the warriors lost by that many points.
If the value is positive,
the warriors won by that amount of points.
The column match-up had
the team, the warriors were playing.
GSW stands for Golden State
and TOR means Toronto Raptors.
Versus signifies it was a home game,
and the @ symbol means an away game.
We can create two DataFrames,
one for the games where the warriors
faced the raptors at home,
and the second for away games.
We can plot out the plus_minus column
for both DataFrames.
We see the warriors played better at home.
(Music)