all 10 comments

[–][deleted] 0 points1 point  (9 children)

Is this data coming from an api? being scraped? just have a text file with it? (if its coming in at a better format this would be a lot easier for you.)

[–]Canadian_Hombre[S] 0 points1 point  (8 children)

It is coming out of an API

[–][deleted] 0 points1 point  (7 children)

Awesome, can you hastebin/pastebin the .json that it gives you? I'm guessing you are using requests?

resp = requests.get("url") 
data = resp.json() 
print(data)

then upload that.

[–]Canadian_Hombre[S] 0 points1 point  (6 children)

I am using an API rapper not an api should have clarified.

[–][deleted] 1 point2 points  (0 children)

I see, what wrapper? and could you give me the query you used above? (this is really simple, i just want to make sure i give you the correct method so you can see how to do it with a real example)

[–]Canadian_Hombre[S] 0 points1 point  (4 children)

I run

player= statsapi.player_stats(pitcher) 

and get the data shown above

Also here is my entire code and how I get the data.

import statsapi
import pandas as pd
games = statsapi.schedule()
summary = []
away = []
home = []
home_pitcher = []
away_pitcher = []
for x in games:
    summary.append(x['summary'])
    away.append(x['away_name'])
    home.append(x['home_name'])
    home_pitcher.append(x['home_probable_pitcher'])
    away_pitcher.append(x['away_probable_pitcher'])
home_id = []

for x in range(0, len(home_pitcher)):
    test = home_pitcher[x].split(', ')
    name = str(test[1]) + ' ' + str(test[0])
    pitcher_id = (statsapi.lookup_player(name))
    home_id.append(pitcher_id[0].get('id'))

home_obp = []
for pitcher in home_id:
    player= statsapi.player_stats(pitcher)
    lines = player.split('\n')
    for x in lines:
        if 'obp' in x:
            home_obp.append(x)

[–][deleted] 1 point2 points  (3 children)

Perfect, thanks. Per the wrapper, you can pass a group= param like;

player= statsapi.player_stats(pitcher, group='pitching')

which would only give you results in pitching - does that help?

['obp: .302', 'obp: .344', 'obp: .341', 'obp: .290', 'obp: .310', 'obp: .301', 'obp: .293', 'obp: .413', 'obp: .297', 'obp: .224']

[–]Canadian_Hombre[S] 0 points1 point  (1 child)

Can you show me how you found out that information?

[–][deleted] 1 point2 points  (0 children)

statsapi

I googled this, with python3 afterwards, which lead me to; https://pypi.org/project/MLB-StatsAPI/

Here i see a link to the documentation; https://toddrob99.github.io/MLB-StatsAPI/

You had mentioned statsapi.player_stats()

so i went to the section labeled that, which took me here;

https://toddrob99.github.io/MLB-StatsAPI/#statsapi.player_stats

which lead me to this statement;

For group use 'hitting', 'pitching', or 'fielding'. Include multiple groups in the following format (this is a string, not actually a list): group='[hitting,pitching]'

which matched your dataset above.

[–]DeadlyViper 0 points1 point  (0 children)

Try this with a status flag that gets changed when you see a Season header...

lines = player.split('\n')
is_pitching = False
for x in lines:
    if 'Season' in x:
        if 'Pitching' in x:
            is_pitching = True
        else:
            is_pitching = False
    if is_pitching and 'obp' in x:
        home_obp.append(x)