See the notes on import csv and/or import pandas before you try these exercises.
You can also get baseball stats in CSV format here:
baseball-reference.com
Go to fangraphs.com (a site about Major League Baseball statistics).
2023 Update: FanGraphs.com no longer allows CSV download unless you become a paid member, so I am providing two examples of CSV files here:
FanGraphs_Leaderboard_2022_Standard.csv
FanGraphs_Leaderboard_2022_Advanced.csv
These files contain batting statistics for the 2022 Major League Baseball season.
Here’s a file with the all time career statistics of players through the 2022 season.
hitting_all_time_through_2022.csv
You will want the relevant file(s) uploaded to your working directory.
You should start by opening this file in a spreadsheet, such as Google Sheets or Microsoft Excel.
(Desktop Excel > Google Sheets > Excel Online)
1) Use either import csv or import pandas to load this data into memory in a Python program.
2) Print out the player name and their Wins Above Replacement (WAR) for the top 20 players on the list. Use a for loop.
3) How many hitters in MLB history have played in at least 500 games (G)?
4) What’s the total number of home runs (HR) that have ever been hit?
5) How many home runs (HR) have been hit by players that have played in fewer than 400 career games (G)?
6) Print out the name of every player that has more defensive Wins Above Replacement (Def) than offensive Wins Above Replacement (Off) in their career. (These are players who provided the most value to their teams while playing defense, rather than as hitters)
Halloween Candy Power Rankings
Download the data for The Ultimate Halloween Candy Power Ranking article from fivethirtyeight.com.
The data can be found on the fivethirtyeight github page. To get the file you want, you will need to click the folder for candy power rankings, then click the “candy-data.csv” file.
Click the “raw” button to get the raw data:
Load the data into Python using the csv or pandas module.
1) How many different candies have chocolate?
2) Print every candy name that has both chocolate and caramel.
3) Print every candy name that has at least three candy categories.
4) How many candies are more than 0.75 on the sugar percentile category and also more than 50% win percentage? Print each of the candies that meets that criteria.
5) Create an equation that describes how YOU rate the candies according to each characteristic. For example, if you really like chocolate, you could add 10 points to your personal candy score, and if you dislike nougat, you could subtract five points.
Create a new file of candies: each line of the file should include the candy name and your calculated candy score for that candy. Bonus: sort the list before you write it to a file so it has your highest rated candies at the top.