<jupyter_start><jupyter_text>WeTeach_Python Day 3 Lesson 1: More Fun with APIs and Data Analysis<jupyter_code>import requests
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import linregress<jupyter_output><empty_output><jupyter_text>We'll begin by using the same API as in Day 2 Lesson 2:<jupyter_code>url = "http://lookup-service-prod.mlb.com/"
teams = requests.get(url + "json/named.team_all_season.bam?sport_code='mlb'&all_star_sw='N'&sort_order='name_asc'&season=2019")
teams
team_data = teams.json()
team_list = team_data['team_all_season']['queryResults']['row']
team_data['team_all_season']['queryResults']['row'][0].keys()<jupyter_output><empty_output><jupyter_text>In this cell, we're pulling all the players from the Padre's 1998 team and adding their player dictionaries to a list.<jupyter_code>batting_stats = []
team_id = [135]

for index, teamid in enumerate(team_id):
  players = requests.get(url + "json/named.roster_team_alltime.bam?start_season=1998&end_season=1998&team_id={}".format(teamid))
  player_list = players.json()['roster_team_alltime']['queryResults']['row']

  for player in player_list:
    hitting_stats = requests.get(url + "json/named.sport_career_hitting.bam?league_list_id='mlb'&game_type='R'&player_id={}".format(player['player_id']))
    hitting_dict = hitting_stats.json()['sport_career_hitting']['queryResults']['row']
    hitting_dict['name'] = player['name_last_first']
    try:
      batting_stats.append(hitting_dict)
    except KeyError:
      pass<jupyter_output><empty_output><jupyter_text>Since we now have a list of batting statistics composed of dictionaries with all the same keys, we can directly create a pandas data frame from that.<jupyter_code>batting_df = pd.DataFrame(batting_stats)

# In this line, we replace the empty strings with NaN (Not a Number)
batting_df_no_empty = batting_df.replace("", np.nan, regex=True)

df_converted = batting_df_no_empty.copy()
for column in batting_df_no_empty.columns:
  try:
    # Here, we convert the appropriate columns to number types
    df_converted[column] = pd.to_numeric(batting_df_no_empty[column])
  except ValueError:
    pass

df_converted<jupyter_output><empty_output><jupyter_text>We can look at the data types by using the `dtypes` attribute:<jupyter_code>display(df_converted.dtypes)
df_converted = df_converted.drop('np', axis='columns')
df_converted<jupyter_output><empty_output><jupyter_text>Some of the players have a lot of `NaN` values. This is a change.<jupyter_code>df_no_na = df_converted.dropna()
df_no_na<jupyter_output><empty_output><jupyter_text>**Your Turn!**Choose two stats to plot in a scatter plot. Then, run a linear regression using `linregress`<jupyter_code><jupyter_output><empty_output><jupyter_text>This is a new cell!<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11377245508982035, 'fraction_numerical': 0.008453680873547023, 'mean_word_length': 7.377581120943953, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '86425', 'license': 'MIT', 'n_tokens_mistral': 1015, 'n_tokens_neox': 983, 'n_words': 215, 'path': 'WeTeach_Python_Day_3_Lesson_1.ipynb', 'repo_name': 'Tic-tics/wtcs_python_example', 'size': '2839'}