<jupyter_start><jupyter_text>Exploratory Data Analysis (EDA) of Olympic games with Pandas<jupyter_code>import pandas as pd
import os
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Download the file `athlete_events.csv` from [Kaggle](https://www.kaggle.com/heesoo37/120-years-of-olympic-history-athletes-and-results) page.<jupyter_code>PATH='../datasets/athlete_events.csv'
print(os.getcwd())
data = pd.read_csv(PATH)
data.head()<jupyter_output><empty_output><jupyter_text>Some preliminary checkup's1. Check data shape - use `shape`. This is equivalent to `dim()` function of R.2. Printing out column names using `columns`. This is equivalent to `colnames()` function of R.3. Use the `info()` method to output some general information about the dataframe. Equivalent to `str()` in R.4. The `describe()` method shows basic statistical characteristics of each numerical feature (int64 and float64 types): number of non-missing values, mean, standard deviation, range, median, 0.25 and 0.75 quartiles. It's equivalent to `summary()` in R.5. To access help on a function, press Shift+Tab or Shift+Tab+Tab keys.<jupyter_code># check data shape
data.shape
# check data columns
data.columns
# check data structure
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 271116 entries, 0 to 271115
Data columns (total 15 columns):
ID        271116 non-null int64
Name      271116 non-null object
Sex       271116 non-null object
Age       261642 non-null float64
Height    210945 non-null float64
Weight    208241 non-null float64
Team      271116 non-null object
NOC       271116 non-null object
Games     271116 non-null object
Year      271116 non-null int64
Season    271116 non-null object
City      271116 non-null object
Sport     271116 non-null object
Event     271116 non-null object
Medal     39783 non-null object
dtypes: float64(3), int64(2), object(10)
memory usage: 31.0+ MB<jupyter_text>The data type `object` is a categorical variable. We can see that there are 10 categorical variables. The data type's, `float64, int64` are continuous in nature. We can see that there are 5 continuous variables<jupyter_code># describe the data
data.describe()<jupyter_output><empty_output><jupyter_text>1. How old were the youngest male and female participants of the 1996 Olympics?<jupyter_code>data[data['Year']==1996].sort_values(by=['Age'], ascending=False).describe()<jupyter_output><empty_output><jupyter_text>For categorical (type object) and boolean (type bool) features we can use the `value_counts` method.<jupyter_code>data['Sex'].value_counts()<jupyter_output><empty_output><jupyter_text>To calculate fractions, pass `normalize=True` to the value_counts function.<jupyter_code>data['Sex'].value_counts(normalize=True)
data.sort_values(by=['Age', 'Sex'], ascending=False).head()
data.sort_values(by=['Age', 'Sex'], ascending=True)<jupyter_output><empty_output><jupyter_text>So, we will use sorting and indexing to retrieve the answer to this question. It's shown below;<jupyter_code>data[data['Year']==1996].sort_values(by=['Age', 'Sex'], ascending=True)<jupyter_output><empty_output><jupyter_text>The youngest participant was <NAME>, Female aged 12 from Cambodia participated in Swimming women's 100m breaststroke.<jupyter_code>data[data['Year']==1996].sort_values(by=['Age', 'Sex'], ascending=False)<jupyter_output><empty_output><jupyter_text>The oldest participant were <NAME> & <NAME>, Male aged 63.  Now, I'll show the usage of two variables to test a condition by applying simple `&` AND operator. So, to test the youngest male participant in the 1996 Olympic games, the code is as follows;<jupyter_code>data[ (data['Year']==1996) & ((data['Sex']=='M')) ].sort_values(by=['Age'], ascending=True)<jupyter_output><empty_output><jupyter_text>From above table, we see that <NAME>-Min from South-Korea aged 14 years in Diving was the youngest male participant. Similarly, for the youngest female participant we can code it as follows;<jupyter_code>data[ (data['Year']==1996) & ((data['Sex']=='F')) ].sort_values(by=['Age'], ascending=True)<jupyter_output><empty_output><jupyter_text>The youngest participant was <NAME>, Female aged 12 from Cambodia participated in Swimming women's 100m breaststroke.  2. What was the percentage of male gymnasts among all the male participants of the 2000 Olympics? Consider only Gymnastics as a target sport. Round the answer to the first decimal. Answer: To answer this question, we'll use both indexing and<jupyter_code># First, list all the sports. Note, sports is a categorical variable. To check levels use `unique()`. In R its `levels()`.  
data['Sport'].unique()
male_gymnast= count(data[( data['Sex']=='M') & (data['Year']==2000)])

#data['perc']= data[( data['Sex']=='M') & (data['Year']==2000)] / data[()]<jupyter_output><empty_output><jupyter_text>3. What are the mean and standard deviation of height for female basketball players participated in the 2000 Olympics? Round the answer to the first decimal.<jupyter_code>print("\n The mean and standard deviation of height for female basketball players participated in the 2000 Olympics")

round(data[( data['Sex']=='F') & (data['Year']==2000) & (data['Sport']=='Basketball')].describe(),1)<jupyter_output>The mean and standard deviation of height for female basketball players participated in the 2000 Olympics<jupyter_text>4. Find a sportsperson participated in the 2002 Olympics, with the highest weight among other participants of the same Olympics. What sport did he or she do?<jupyter_code>data[data['Year']==2002].sort_values(by=['Sex','Weight'], ascending=False)<jupyter_output><empty_output><jupyter_text>Answer 4) <NAME>, Male aged 26 Weight 123kg was heaviest among other participants, played Bobsleigh  5. How many times did <NAME> participate in the Olympics held in different years?<jupyter_code>#data[data['Name']=='<NAME>'].value_counts()
data.groupby(['Year'])['Name'].value_counts()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11803934644881627, 'fraction_numerical': 0.04034678226075358, 'mean_word_length': 6.433705080545229, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 68, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '368465', 'license': 'MIT', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1792, 'n_words': 607, 'path': 'python/machine learning/Olympic Games EDA.ipynb', 'repo_name': 'duttashi/stackoverflow-notes', 'size': '6060'}