Today, data analysis sits at the heart of every Major League Baseball (MLB) organisation. For those of us who can't manage a real baseball team, fantasy baseball offers a fun and accessible way to apply the same analytical ideas.
This post kicks off a short series showing how Python can be used to analyse MLB data, with the practical goal of helping you build a successful fantasy baseball team.
For this series, I will be using Jupyter in VS Code. If you do not already have this set up, you can find a guide to getting started with Jupyter on our blog here: https://www.thedataschool.co.uk/a/garth-turner/python-any-easy-way-to-get-started/
We will be working with the Python API wrapper known as MLB-StatsAPI, which retrieves real-time statistics from the official MLB Stats API. Installation instructions and documentation can be found on the project’s GitHub page: https://github.com/toddrob99/MLB-StatsAPI/wiki
Today, we will learn how to retrieve the batting statistics for a single player. To begin, create a new Jupyter cell and import the statsapi library.

Next, create another Jupyter cell and search for a player by name. In this example, we will look up Seattle Mariners' power hitter Cal Raleigh.

In the code above, we search for the player using the statsapi.lookup_player() function. The result is stored in a variable called player. When printed, this returns a list of dictionaries, with this type of output:

What we need is the player’s unique ID. Since this is a list, we can take the first match using [0] and extract the id field. We can then use this ID to request the player’s batting statistics. In a new cell, we can run the code below.

Here, we extract the player ID and store it in a new variable called player_id. We then call statsapi.player_stats() to retrieve the player’s hitting statistics. Because Cal Raleigh is a hitter, we use the hitting group. By setting the type argument to season, the API returns statistics for the most recent season by default. The output is shown below.

And we have our stats. Despite a relatively modest batting average of .247, Cal Raleigh hit a record breaking 60 home runs in 2025, alongside an excellent OPS of .948.
In the next post, we will look at how to compare the stats of two players, helping us find the strongest candidates for our fantasy baseball team.
Note: If you’re unfamiliar with these stats, MLB provides a helpful glossary here: https://www.mlb.com/glossary/standard-stats
