How to find baseball player similarities using python pandas or dask?

Viewed 89

Here are the rules formula for finding similarities between baseball players:

One point for each difference of 20 games played.
One point for each difference of 75 at bats.
One point for each difference of 10 runs scored.
One point for each difference of 15 hits.
One point for each difference of 5 doubles.
One point for each difference of 4 triples.
One point for each difference of 2 home runs.
One point for each difference of 10 RBI.
One point for each difference of 25 walks.
One point for each difference of 150 strikeouts.
One point for each difference of 20 stolen bases.
One point for each difference of .001 in batting average.
One point for each difference of .002 in slugging percentage.

I have some players like:

Some Players value

Can you please explain me how to make the formula using pandas or dask in python? I am stuck with it.

1 Answers

Not quite sure what you are asking, so going to make an assumption that these "points" apply to each player/row, then you can find similar players by looking for close scores.

To do this, simply setup a loop to calculate this out (I'd do as by vectorized column because it's faster as opposed to iterating by rows to do it.)

You may need to adjust the column names to match yours in your data set in the rules dictionary I created.

import pandas as pd
import requests
import numpy as np


# Sample Data
mlb_api = 'https://bdfed.stitch.mlbinfra.com/bdfed/stats/player'
payload = {
'stitch_env': 'prod',
'season': '2020',
'sportId': '1',
'stats': 'season',
'group': 'hitting',
'gameType': 'R',
'offset': '0',
'sortStat': 'onBasePlusSlugging',
'order': 'desc'}

jsonData = requests.get(mlb_api, params=payload).json()
df = pd.DataFrame(jsonData['stats'])


# Here is the Code you'll need
rules = {
'gamesPlayed':20,
'atBats':75,
'runs':10,
'hits':15,
'doubles':5,
'triples':4,
'homeRuns':2,
'rbi':10,
'baseOnBalls':25,
'strikeOuts':150,
'stolenBases':20,
'avg':.001,
'slg':.002}


df['Score'] = 0
for col, points in rules.items():
    df['Score'] += np.floor(df[col].astype(float) / points)

df = df.sort_values('Score', ascending=False).reset_index(drop=True)

Output:

print(df[['playerName','Score']])
            playerName  Score
0            Juan Soto  719.0
1      Freddie Freeman  691.0
2        Marcell Ozuna  687.0
3          DJ LeMahieu  680.0
4           Jose Abreu  658.0
5          Trea Turner  657.0
6        Dominic Smith  646.0
7         Jose Ramirez  623.0
8          Nelson Cruz  623.0
9         Corey Seager  623.0
10       Manny Machado  622.0
11           Wil Myers  614.0
12           Luke Voit  610.0
13          Mike Trout  607.0
14    Mike Yastrzemski  602.0
15    Michael Conforto  600.0
16   Teoscar Hernandez  600.0
17        Eloy Jimenez  598.0
18        Mookie Betts  597.0
19  Fernando Tatis Jr.  590.0
20        Brandon Lowe  568.0
21    Ronald Acuna Jr.  562.0
22        Bryce Harper  561.0
23     George Springer  556.0
24      Anthony Rendon  552.0
Related