Lineup Optimizer Part 2

Quality of Life Improvements

Starting Code as .ipynb (Jupyter Notebook)

If you haven't watched the first video creating the optimizer, please watch that first!!

-------FIRST VIDEO--------
https://youtu.be/zibV6xaOGEA
-----------------------------------

In this video we are going to be making a few quality of life improvements to our initial lineup optimizer! Namely, we are going to stay within the Pandas framework rather than writing each lineup straight to excel. This is going to make review and manipulation MUCH easier and more efficient.

----NEW WORKFLOW---

What changes from the original?? Well for starters we don't need to import openpyxl anymore, and we can disregard any code referring to that package. Instead of keeping count of which column we are writing to in excel, and restarting that count at 1 for each new lineup, we are simply going to establish a python dictionary to store the lineups in. This dictionary will have the lineup integer as the key, and a list containing the players selected for the lineup as the value:

{
1: [PG, PG, SG, SG, SF, SF, PF, PF, C],
2: [PG, PG, SG, SG, SF, SF, PF, PF, C],
… etc.
}

This will prove to be a much more efficient way to store our data. Once the dictionary is created with however many lineups you are interested in, we simply define a pandas dataframe with this dictionary as the data source. Pandas will automatically pull in the information, albeit with the lineup numbers as column headers. However we would prefer each lineup to read horizontal across the dataframe rather than vertical, so we will need to perform a Transpose function on the dataframe. This merely switches the rows and columns, so the lineup number will be the index, and each lineup will read as its own row rather than its own column.

Now that we have the dataframe set up how we want, we need to start doing some cleanup on these values!! We need to remove the positional argument and underscore from the beginning of each player id:

"C_1234_12345", "PF_123_34543"

and make it look like this:

"1234-12345", "123-34543".

The easiest(not the best or most efficient, mind you) way to do this is to simply run a nested for loop over the positional columns and first remove the specified positional arguments with the underscore, leaving us with values like

"1234_12345", "123_34543",

and then simply running a replace function over the remaining values to replace the underscore with a hyphen. Once this is done, our player ids are in the correct format to bulk upload to Fanduel!

Next, we need to reorganize our column headers so we don't have to copy and paste individual columns over and we can just copy and paste the whole thing because our players are already in the correct order of

["PG", "PG", "SG", "SG", "SF", "SF", "PF", "PF", "C"].

Next, we can create a quick dictionary of player ids as the key and player names as the value using our original dataframe we created to run the optimizer. Pandas has a very easy to use function that will run through your dataframe and if the keys of a dictionary are present, it will replace the dataframe values with the dictionary value associated with that key. This will replace all of our player ids with player names for easier review.

Now you may notice that a lot of the players are repeated. That is not *necessarily* a bad thing, however we will address why that is and what we can do about it in the next video so make sure to subscribe if you aren't already so you don't miss it! If you see something wrong in the code, or have some thoughts on ideas or additions you would like to see in the future, let me know in the comments!

Previous
Previous

Lineup Optimizer Part 3

Next
Next

Lineup Optimizer Part 1