1

I am learning to web scrape using MLB data. I would just like the team and the WAR data. I am not sure how to parse only the data that I am looking for. I do not desire the team record or the parenthesis. Any help would be greatly appreciated.

My hope is to create a Pandas DataFrame with the desired output.

Data needed: 1) Team 2))WAR

Desired data format(below):

Team WAR

ARI 1.3

ATL 1.87

BAL 2.60

BOS .43

enter image description here ...

import pandas as pd

url = 'https://www.baseball-reference.com/leagues/MLB/2020-team-starting-lineups.shtml'

test = pd.read_html(url)

for t in test:
    team = t['Tm']    
    print(team)

I am not sure how to parse out the extra data. Thanks in advance for your time and suggestions. =)

1 Answer 1

2

You can use Series.str.extract to get the information from the column:

import pandas as pd

url = 'https://www.baseball-reference.com/leagues/MLB/2020-team-starting-lineups.shtml'

test = pd.read_html(url)

for t in test:
    team = t['Tm'].str.extract(r'^(?P<Team>[A-Z]+).*?(?P<WAR>[^\s]+)\s*oWAR')
    print(team)

Prints:

   Team    WAR
0   ARI   1.31
1   ATL   1.87
2   BAL   2.60
3   BOS   0.43
4   CHC   2.64
5   CHW   3.09
6   CIN   0.75
7   CLE  -0.81
8   COL   1.89
9   DET   1.40
10  HOU   1.64
11  KCR   0.74
12  LAA   0.58
13  LAD   4.51
14  MIA   1.34
15  MIL  -1.00
16  MIN   2.66
17  NYM   3.58
18  NYY   3.24
19  OAK   3.11
20  PHI   2.12
21  PIT  -1.89
22  SDP   2.65
23  SEA   0.67
24  SFG   1.05
25  STL   0.31
26  TBR   3.26
27  TEX   0.26
28  TOR   0.87
29  WSN   0.11
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir. Is that regex? I am not familiar with that style of coding but grateful nonetheless. I will try to study it and thankful for the learning experience. I will vote this answer as the best if there are no alternative that are superior. Thanks again for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.