from pandas import DataFrame, read_csv, cut
df = read_csv('data/baseball_data.csv')
df.describe()

"""
Explanation: Summary
This dataset about information of baseball players. 
It includes player's name, handedness (right, left handed, or both), height, weight, batting average, and the number of home runs.
Design
Exploratory Data Analysis using Python
I downloaded baseball_data.csv the dataset from data set options page given by Udacity. The dataset had player's physical information(name, height, weight) and performance information(avg, HR).
End of explanation
"""


df = df[(df.avg > 0.0) & (df.HR > 0)]
import numpy as np
df['avg_category'] = cut(df.avg, bins = np.linspace(0.1, 0.35, 6), 
                         right=False)
df.describe()

"""
Explanation: There are 1157 players' records. Interestingly, some players have zero batting average or home runs, so I removed those rows.
End of explanation
"""


a = df.drop(['height', 'weight', 'avg'], axis=1).groupby(by=['avg_category', 'handedness']).sum()
a

a.to_csv('data/cleaned_baseball.csv')

df = read_csv('data/cleaned_baseball.csv')
subtotal = df.groupby('avg_category', as_index=False).sum()
subtotal

final_df = df.merge(subtotal, on='avg_category')

final_df['HR_ratio'] = final_df.HR_x / final_df.HR_y
final_df

final_df.to_csv('data/cleaned_baseball.csv')

df = read_csv('data/cleaned_baseball.csv')
print df
submean = df.drop(['Unnamed: 0']).groupby('avg_category', as_index=False).mean()

df.merge(submean, on='avg_category')

"""
Explanation: After cleaning meaningless rows, I had 871 records. Now I wanted to figure out player's performance with different handedness and avg_category. I showed histogram, HR vs. avg_category with 3 types of handedness.
End of explanation
"""


