<jupyter_start><jupyter_text><NAME>  DATA SCIENCE AND BUSINESS ANALYTICS INTERN @ tHE SPARKS FOUNDATION  GRIPJUNE21 TSF   Exploratory Data Analysis - Sports  Dataset used- ‘Indian Premier League  Objectives:1. To find the team that won the most number of matches in a season.2. To find the team that lost the most number of matches in a season.3. Does winning toss increases the chances of victory.4. To find the player with the most player of the match awards.5. To find the city that hosted the maximum number of IPL matches.6. To find the most winning team for each season.7. To find the on-field umpire with the maximum number of IPL matches.8. To find the biggest victories in IPL while defending a total and while chasing a total.<jupyter_code>import pandas as pd
import numpy as np
url= "matches.csv"
df=pd.read_csv(url)
import warnings
warnings.filterwarnings("ignore")
df.head()<jupyter_output><empty_output><jupyter_text>The dataset(matches) has 18 columns.  lets explore each variable:id: The IPL match id.    season: The IPL seasoncity: The city where the IPL match was held.date: The date on which the match was held.team1: One of the teams of the IPL matchteam2: The other team of the IPL matchtoss_winner: The team that won the tosstoss_decision: The decision taken by the team that won the toss to ‘bat’ or ‘field’result: The result(‘normal’, ‘tie’, ‘no result’) of the match.dl_applied: (1 or 0)indicates whether the Duckworth-Lewis rule was applied or not.winner: The winner of the match.win_by_runs: Provides the runs by which the team batting first wonwin_by_runs: Provides the number of wickets by which the team batting second won.player_of_match: The outstanding player of the match.venue: The venue where the match was hosted.umpire1: One of the two on-field umpires who officiate the match.umpire2: One of the two on-field umpires who officiate the match.umpire3: The off-field umpire who officiates the match<jupyter_code>df.shape
url2 = "deliveries.csv"
data = pd.read_csv(url2)
data.head()
data.shape
df.info()
df.describe()<jupyter_output><empty_output><jupyter_text>Observations:there is a difference between mean and median(50%tile) value of above variablesalso there is a difference between max and 75%tile values of above variablesIt means some outliers are Present.Observations:The following inferences can be made from the describe() method:The .csv file has data of IPL matches starting from the season 2008 to 2019.The biggest margin of victory for the team batting first(win_by_runs) is 146 runs.The biggest victory of the team batting second(win_by_wickets) is by 10 wickets.75% of the victorious teams that bat first won by a margin of 19 runs.75% of the victorious teams that bat second won by a margin of 6 wickets.There were 756 IPL matches hosted from 2008 to 2019.  Since the 'date' is not relevant and does not provide any additional insights for our analysis, we will drop this column.   umpire1, umpire2, and umpire3 can also be dropped for same reason.<jupyter_code>df.drop(['date', 'umpire1', 'umpire2', 'umpire3'], axis=1, inplace=True)
df.isnull().sum()
df=df.replace(np.nan,0)
data.isnull().sum()
# there are more than 90% null values in 'player_dismissed','dismissal kind' and 'fielder' so drop these variables
data.drop(['player_dismissed','dismissal_kind','fielder'], axis=1, inplace=True)
#Check again for null values
data.isnull().sum()
# set id as the index column
df.set_index('id', inplace=True)
# set match_id as the index column
data.set_index('match_id', inplace=True)
# merge (id,season,winner) columns in deliveries dataset which will be helpful in further analysis of data.
new_data=df[['season','winner']]

complete_data = data.merge(new_data, how='inner',left_on = data.index, right_on = df.index)
complete_data.head()
complete_data.isnull().sum()
#it contains no null values<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis  To check Correlation using HeatMap<jupyter_code>import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure(figsize=(20,8))
sns.heatmap(data.corr(),cmap='Blues',annot=True)
# by setting annot=True you will get values by ehich variables are correlated to each other in grid-cells
df.head(20)
df['season'].value_counts()<jupyter_output><empty_output><jupyter_text>In which season highest number of matches were played?<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(df['season'])
plt.xticks(rotation=90)
df['season'].value_counts()<jupyter_output><empty_output><jupyter_text>In which City maximum matches were played?<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(df['city'])
plt.xticks(rotation=90)
df['city'].value_counts()<jupyter_output><empty_output><jupyter_text>Teams we have in IPL<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(df['team1'])
plt.xticks(rotation=90)
df['team1'].value_counts()<jupyter_output><empty_output><jupyter_text>Result: Mumbai India has won max of toss that is why they mostly played as Team1<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(df['team2'])
plt.xticks(rotation=90)
df['team2'].value_counts()<jupyter_output><empty_output><jupyter_text>Which team has won Maximum of Toss?<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(df['toss_winner'])
plt.xticks(rotation=90)
df['toss_winner'].value_counts()
plt.figure(figsize=(7,2))
sns.countplot(df['toss_decision'])
plt.xticks(rotation=90)
df['toss_decision'].value_counts()<jupyter_output><empty_output><jupyter_text>Result: Team mostly select Feilding than Batting.  What is the Result of the match?<jupyter_code>plt.figure(figsize=(10,5))
sns.countplot(df['result'])
plt.xticks(rotation=90)
df['result'].value_counts()<jupyter_output><empty_output><jupyter_text>How Many times Duckworth Lewis method was applied?<jupyter_code>plt.figure(figsize=(10,5))
sns.countplot(df['dl_applied'])
plt.xticks(rotation=90)
df['dl_applied'].value_counts()<jupyter_output><empty_output><jupyter_text>Result: 19 times between 2008 to 2019  Which team has mostly won the match?<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(df['winner'])
plt.xticks(rotation=90)
df['winner'].value_counts()<jupyter_output><empty_output><jupyter_text>By how many Runs they have mostly won?<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(df['win_by_runs'])
plt.xticks(rotation=90)
df['win_by_runs'].value_counts()<jupyter_output><empty_output><jupyter_text>Won By Wickets<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(df['win_by_wickets'])
plt.xticks(rotation=90)
df['win_by_wickets'].value_counts()<jupyter_output><empty_output><jupyter_text>REsult:  Team has mostly won by 6 wickets<jupyter_code>plt.figure(figsize=(60,20))
sns.countplot(df['player_of_match'])
plt.xticks(rotation=90)
df['player_of_match'].value_counts()<jupyter_output><empty_output><jupyter_text>Match Mostly played in which Stadium?<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(df['venue'])
plt.xticks(rotation=90)
df['venue'].value_counts()<jupyter_output><empty_output><jupyter_text>Winner of each Season:<jupyter_code>season_winner = df.drop_duplicates(subset=['season'], keep='last')[['season', 'winner']]
season_winner.sort_values(by="season").reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>Which team has played maximum Matches?<jupyter_code>max_matches = pd.DataFrame(
{'Total Matches Played': df["team1"].value_counts() + df["team2"].value_counts(),
 'Total Matches Won': df["winner"].value_counts(), 
 'Total Matches Lost': ((df['team1'].value_counts() + df['team2'].value_counts())-df["winner"].value_counts())
})
max_matches.sort_values(by="Total Matches Played", ascending=False)<jupyter_output><empty_output><jupyter_text>Relation between toss and match winners?<jupyter_code>df_toss = df['toss_winner'] == df["winner"]
ax = df_toss.value_counts().plot(kind="pie", autopct='%1.1f%%', shadow=True)<jupyter_output><empty_output><jupyter_text>Result:This piechart shows that winning toss actually helped to win the match as well by 51.3%  Working on Deliveries Dataset.<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(data['inning'])
plt.xticks(rotation=90)
data['inning'].value_counts()<jupyter_output><empty_output><jupyter_text>Which Team has done Maximum Batting?<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(data['batting_team'])
plt.xticks(rotation=90)
data['batting_team'].value_counts()<jupyter_output><empty_output><jupyter_text>Which Team has done Maximum Bowling?<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(data['bowling_team'])
plt.xticks(rotation=90)
data['bowling_team'].value_counts()<jupyter_output><empty_output><jupyter_text>Over-Over Number<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(data['over'])
plt.xticks(rotation=90)
data['over'].value_counts()<jupyter_output><empty_output><jupyter_text>ball- Ball Number in the over<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(data['ball'])
plt.xticks(rotation=90)
data['ball'].value_counts()<jupyter_output><empty_output><jupyter_text>batsman- Name of the batsman at crease<jupyter_code>data['batsman'].value_counts()<jupyter_output><empty_output><jupyter_text>name of Batsman at non striker end<jupyter_code>data['non_striker'].value_counts()<jupyter_output><empty_output><jupyter_text>bowler- Bowler who bowled the over<jupyter_code>data['bowler'].value_counts()<jupyter_output><empty_output><jupyter_text>Players with most 4s:<jupyter_code>data[data["batsman_runs"] == 4].groupby("batsman")["batsman_runs"].count().sort_values(ascending = False).head(10).plot(kind='bar', title="Top Players with most 4s", figsize=(15,7))<jupyter_output><empty_output><jupyter_text>Players with most 6s:<jupyter_code>data[data["batsman_runs"] == 6].groupby("batsman")["batsman_runs"].count().sort_values(ascending = False).head(10).plot(kind='bar', title="Top Players with most 6s", figsize=(15,7))<jupyter_output><empty_output><jupyter_text>Batsman with top bat runner  score:<jupyter_code>top_batsman=data.groupby('batsman')['batsman_runs'].agg(['sum']).reset_index().sort_values('sum',ascending=False).reset_index(drop=True)
top_batsman=top_batsman.rename(columns={'sum':'batsman_runs'})
top_batsman.iloc[:10,:]<jupyter_output><empty_output><jupyter_text>Penalty Runs:<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(data['penalty_runs'])
plt.xticks(rotation=90)
data['penalty_runs'].value_counts()<jupyter_output><empty_output><jupyter_text>Total_Runs: Total_Extra+Runs by batsman<jupyter_code>plt.figure(figsize=(17,5))
sns.countplot(data['total_runs'])
plt.xticks(rotation=90)
data['total_runs'].value_counts()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13056603773584904, 'fraction_numerical': 0.020283018867924527, 'mean_word_length': 8.779520295202952, 'pattern_counts': {'":': 0, '<': 130, '<?xml version=': 0, '>': 130, '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': '32', 'dir': 'jupyter-structured-clean-dedup', 'id': '589040', 'license': 'Apache-2.0', 'n_tokens_mistral': 3924, 'n_tokens_neox': 3715, 'n_words': 833, 'path': 'Retail_Spark_Internship.ipynb', 'repo_name': 'aishwaryaa-01/Sports_EDA_Spark', 'size': '10610'}