How to Moneyball the s**t out of your fantasy league using Python (Part 1)

Hello there,

So that time of the year came a month ago or so, when my friends started the new season of our NBA fantasy league. I have to say that I consider myself a NBA fan but still, this kind of tournaments do not make any sense to me at first sight, the numbers do not work as they should, players tend to be overvalued or undervalued by a lot.

But this year is going to be different, I am going to show you how after 4 weeks I am already winning the league by far, even though I have not seen a single game so far, using simple data analysis with Python.

Let’s Start coding!

First of all, we need to check the API from our league, in my case it is open, you just need to open the webpage with the network tab open and look for the word API.

There you can find a data file that usually contains all the data needed for the analysis (players scores, value, price, etc.)

Once you get the correct URL you can use the library requests to convert that information to a JSON file and from the dictionary that the method returns, create a pandas DataFrame.

After this and a few index recalculations, we can show the data structure correctly using the head of the DataFrame.

Creating Variables

Even though we already have a lot of different variables to work with, when it comes to buying players what we are more interested on is the average of points they produce each game, and how many points they produce per coin (or whatever currency you are using in your league)

My case is a bit complex as most of the information is not open, but I can calculate the total number of games the player has played using the games played at home plus the played away, and then calculating the total points the same way (the total points you see in the DataFrame are relative to weeks and we are interested in the per game points).

As you can see I have used a MinMaxScaler, this is not needed, but it makes the variable more readable.

Plotting and quick analysis

So to start getting proper knowledge from the data, let’s plot the players price against its point per game.

This scatter plot already give us an idea of how the prices evolve, and can help us identify quickly some players that shine above the rest (we can see Alec Burks with a low price for its performance).

But still when it comes to specific players it might be difficult to identify in the middle of that madness what we are looking for.

Specific player analysis

The solution I came for to make an analysis of how valuable a player is, was to create different ranges for points per game and divide the players by it.

Once we have the different groups, we can analyze how a player pointsPerCoin score is compared to the other players in the same range, showing us if it is worth the buy or not.

In my case, I create a mean score of of pointsPerCoin in that pointsPerGame range and compare the value of the player with that mean, if the value is negative, the player might not be worth buying, it is positive will have good performance for its value.

So if we look for a specific player using this function we will get how it behaves for its points per game. Here is a example using Josh Okogie.

So as you can see in this case, we should be able to find better players in the range of 10–15 PPG.

And that is it for today!

Hope you liked it and if someone is interested, you can find the entire code in my github account.

Data Scientists with a passion for Sports and Sports Data Analysis.