<jupyter_start><jupyter_text>On Base Percentage![On Base Percentage](https://upload.wikimedia.org/math/5/6/e/56e63a7b1b0724813e09ba0ab1160a22.png)obp = (hits + walks + hit_by_pitch) / (at_bats + walks + hit_by_pitch + sacrifice_flies)<jupyter_code>appearances = pd.read_csv('data/baseballdatabank-master/core/Appearances.csv')
positions = pd.Series(find_position(places))
appearances = appearances[['yearID', 'teamID', 'playerID']].copy()
appearances['position'] = positions
appearances.tail()
batting_salaries_by_position = pd.merge(batting_salaries, appearances)
batting_salaries_by_position = batting_salaries_by_position.fillna(0.0)
batting_salaries_by_position['OBP'] = on_base_percentage(batting_salaries_by_position.pop('H'),
                                                         batting_salaries_by_position.pop('BB'),
                                                         batting_salaries_by_position.pop('HBP'),
                                                         batting_salaries_by_position.pop('AB'),
                                                         batting_salaries_by_position.pop('SF')) 
#yearly_data = batting_salaries_by_position.groupby(['yearID', 'playerID'])
#yearly_data.groups
salary_avg = numpy.mean(batting_salaries_by_position['salary'])
obp_avg = numpy.mean(batting_salaries_by_position['OBP'])
salary_distance = []
obp_distance = []
for row in batting_salaries_by_position.itertuples():
    salary_distance.append(salary_avg - row.salary)
    obp_distance.append(obp_avg - row.OBP)

batting_salaries_by_position['salary_distance'] = pd.Series(salary_distance)
batting_salaries_by_position['obp_distance'] = pd.Series(obp_distance)
batting_salaries_by_position.tail()
batting_salaries_by_position['OBP'].plot.hist()
moneyball = batting_salaries_by_position.groupby(['salary_distance', 'obp_distance'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11294619072988812, 'fraction_numerical': 0.013851891315929675, 'mean_word_length': 4.0894308943089435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '452509', 'license': 'MIT', 'n_tokens_mistral': 630, 'n_tokens_neox': 601, 'n_words': 60, 'path': 'moneyball.ipynb', 'repo_name': 'gcrowder/moneyball', 'size': '1877'}