Welcome to part 3 of my blog series about analysing baseball data in Python to help us find the best value players for our fantasy baseball team. Click on my profile on this page to see parts one and two.
Today we'll be creating our own "fantasy score." We'll use this to evaluate hitters based on four hitting statistics:
- On Base Percentage Plus Slugging (OPS)
- Runs Batted In (RBI)
- Stolen Bases (SB)
- Total Bases (TB)
Refer to the MLB glossary for more details on these: https://www.mlb.com/glossary/standard-stats
I chose these four stats because they capture power (TB), overall hitting ability (OPS), run production (RBI), and speed (SB).
Our code will find the top 100 ranked players for each of these stats and place them into tables. We'll then combine these into one table that adds together each player's ranks across the four stats, allowing us to see which hitters perform well across the board.
Players who perform well in multiple categories will receive a lower combined rank and therefore rank higher overall.
Here is the code for today:
As per last week, we begin by importing our libraries and setting the season and top_n variables:

Next, we get the top 100 hitters by OPS, in a similar manner to last week.

We then get the top 100 players by stolen bases.

Next, we get the top 100 players by RBI.

Our last stat is total bases.

Next, we join these four stat tables we just made into one. For any players who are ranked in the top 100 for some stats but not others, we assign them a default rank of 101st for any stat where they do not appear in the top 100.

Finally, we create our combined ranking by adding each player's ranks together. We then sort by this newly created rank and export the results to a CSV file so we can easily open it in Excel.

Now we can open the CSV and see which hitters perform well across our four hitting stats. This CSV contains hundreds of hitters ranked by our very own fantasy score that we created. This will allow us to find good value hitters for our team!
Keep your eyes peeled for the next installment of the blog, as we prepare to make our data into a Tableau dashboard to help us visualise the data and make better choices for our fantasy team.
