text_plain_output_1.png
import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import numpy as np
import pandas as pd
import seaborn as sns
Deliveries_Data = pd.read_csv('../input/deliveries_expanded.csv')
Matches_Data = pd.read_csv('../input/matches_expanded.csv')

Overall_Matches_State = pd.Series.to_frame(Matches_Data['State'].value_counts())
Overall_Matches_State['%Contribution'] = Overall_Matches_State['State'] / Overall_Matches_State['State'].sum() * 100

Overall_Matches_Zone = pd.Series.to_frame(Matches_Data['Zone'].value_counts())
Overall_Matches_Zone['%Contribution'] = Overall_Matches_Zone['Zone'] / Overall_Matches_Zone['Zone'].sum() * 100

import matplotlib.pyplot as plt
Matches_understood_by_year =  pd.Series.to_frame(Matches_Data.groupby('season')['Zone'].value_counts())
Plot_Zone = Matches_Data.groupby('season')['Zone'].value_counts().unstack().plot(kind="bar", figsize = (15,5))
print(Plot_Zone.get_legend().set_bbox_to_anchor((0.15,1)))

Matches_Data.groupby('city')['Winner_Zone_Type'].value_counts().unstack().dropna().plot(kind='bar', figsize=(15, 5))

Distinct_Team = Matches_Data['team1'].unique()
Total_Teams = len(Distinct_Team)
for i in range(Total_Teams):
    Team_Name = Distinct_Team[i]
    Matches_Data_Toss_Comparision = Matches_Data[['city', 'Zone', 'toss_winner', 'toss_decision', 'Toss_Win_Zone', 'winner', 'Winner_Zone']]
    Matches_Data_Toss_Comparision = Matches_Data_Toss_Comparision[Matches_Data_Toss_Comparision['toss_winner'] == Team_Name]
    Matches_Data_Toss_Comparision['Win_comparison'] = Matches_Data_Toss_Comparision.apply(lambda x: 'Win' if x['toss_winner'] == x['winner'] else 'Lost', axis=1)

Deliveries_Data.head(2)

Deliveries_Powerplay = Deliveries_Data.loc[Deliveries_Data["over"] < 7]
#Delivery_team = Deliveries_Powerplay[Deliveries_Powerplay["batting_team"] == "Sunrisers Hyderabad"]
Deliveries_team_analysed = Deliveries_Powerplay.groupby(['batting_team','over'])['total_runs'].sum().unstack().plot(kind="bar",figsize = (15,5))
Deliveries_team_analysed.get_legend().set_bbox_to_anchor((0.18,1))
#Deliveries_team_analysed
#.sort_values(by ="total_runs", ascending = False)

Contribution_data = Deliveries_Data.groupby(['batting_team', 'over_type'])['total_runs'].sum().unstack()
Contribution_data['%Runs_Powerplay'] = Contribution_data['Powerplay_Over'] / (Contribution_data['Death_Over'] + Contribution_data['Middle_Over'] + Contribution_data['Powerplay_Over']) * 100
Contribution_data['%Runs_MiddleOvers'] = Contribution_data['Middle_Over'] / (Contribution_data['Death_Over'] + Contribution_data['Middle_Over'] + Contribution_data['Powerplay_Over']) * 100
Contribution_data['%Runs_DeathOvers'] = Contribution_data['Death_Over'] / (Contribution_data['Death_Over'] + Contribution_data['Middle_Over'] + Contribution_data['Powerplay_Over']) * 100
Contribution_data
Plot = Contribution_data[['%Runs_Powerplay', '%Runs_MiddleOvers', '%Runs_DeathOvers']].sort_values(by='%Runs_DeathOvers', ascending=False).plot(kind='bar', stacked=True, figsize=(15, 5))

for i in range(Total_Teams):
    Team_Name = Distinct_Team[i]
    Team_Data = Deliveries_Data[Deliveries_Data['batting_team'] == Team_Name]
    Deliveries_Type_defined = Team_Data.groupby(['batting_team', 'over_type'])['total_runs'].sum().unstack()
    Deliveries_Type_defined['Death_Over'].sum() / (Deliveries_Type_defined['Death_Over'].sum() + Deliveries_Type_defined['Middle_Over'].sum() + Deliveries_Type_defined['Powerplay_Over'].sum())
    Deliveries_Type_defined = pd.Series.to_frame(Deliveries_Type_defined.sum()).reset_index()
    Column_Names = ['Over_Type', 'Total_Score']
    Overtype_defined_names = pd.DataFrame(data=Deliveries_Type_defined.values, columns=Column_Names)
    Overtype_defined_names['%Contribution'] = Overtype_defined_names['Total_Score'] / Overtype_defined_names['Total_Score'].sum() * 100

Deliveries_Data.columns

Deliveries_extras_analysed = Deliveries_Data[['batting_team', 'bowling_team', 'over_type', 'bowler', 'batsman', 'wide_runs', 'bye_runs', 'legbye_runs', 'noball_runs', 'penalty_runs', 'batsman_runs', 'extra_runs', 'total_runs']]

Extra_Columns = ['batting_team', 'bowling_team', 'over_type', 'bowler', 'batsman', 'wide_runs', 'bye_runs', 'legbye_runs', 'noball_runs', 'penalty_runs', 'batsman_runs', 'extra_runs', 'total_runs']
Extra_Columns
Deliveries_extras_analysed['Total_Extras'] = Deliveries_extras_analysed[Extra_Columns].sum(axis=1)
Extras_Over_type = Deliveries_extras_analysed.groupby(['bowling_team', 'over_type'])['extra_runs'].sum().unstack()
Extras_Runs = Deliveries_extras_analysed.groupby(['bowling_team', 'batting_team'])['extra_runs'].sum().unstack().fillna(0)

Runs_Values_Analysed = ["wide_runs","bye_runs","legbye_runs","noball_runs","penalty_runs","batsman_runs","extra_runs","total_runs"]
Total_Extras = len(Runs_Values_Analysed)
for i in range(Total_Extras):
    Extra_Name = Runs_Values_Analysed[i]
    print(Extra_Name)
    #Deliveries_extras_analysed = Deliveries_extras_analysed[Deliveries_extras_analysed[Extra_Name]]
    Extras_By_team = Deliveries_extras_analysed.groupby(['bowling_team',Extra_Name])['extra_runs'].sum().unstack().fillna(0)
    Extras_By_team_Columns = Extras_By_team.columns
    String = "Total"+Extra_Name
    Extras_By_team[String] = Extras_By_team[Extras_By_team_Columns].sum(axis=1)
    Extras_By_team = Extras_By_team.sort_values(by=String, ascending = False)
    String_Plot = "Plot"+Extra_Name
    String_Plot = Extras_By_team[String].plot(kind = "bar",figsize=((15,5)))
    plt.xlabel('Toss_decision, Zone',fontsize = 10)
    #plt.ylabel('Ticket Count',fontsize = 10)
    plt.title(Team_Name,fontsize = 15)
    print(plt.show())

All_2_Detailed = Deliveries_extras_analysed[Deliveries_extras_analysed['batsman_runs'] == 2]
All_2_Detailed.head(2)
All_3_Detailed = Deliveries_extras_analysed[Deliveries_extras_analysed['batsman_runs'] == 2]
All_3_Detailed.head(2)
All_4_Detailed = Deliveries_extras_analysed[Deliveries_extras_analysed['batsman_runs'] == 4]
All_4_Detailed.head(2)
All_5_Detailed = Deliveries_extras_analysed[Deliveries_extras_analysed['batsman_runs'] == 5]
All_5_Detailed.head(2)
All_6_Detailed = Deliveries_extras_analysed[Deliveries_extras_analysed['batsman_runs'] == 6]
All_6_Detailed.head(2)
All_7_Detailed = Deliveries_extras_analysed[Deliveries_extras_analysed['batsman_runs'] == 7]
All_7_Detailed.head(2)
All_8_Detailed = Deliveries_extras_analysed[Deliveries_extras_analysed['batsman_runs'] == 8]
All_8_Detailed.head(2)