# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="z4OD_y1WUd2h"
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import statistics 
import math

# + id="3eiNaH1RUiIY" outputId="a4926e32-e5ae-4580-81f2-77c191cf6dc6" colab={"base_uri": "https://localhost:8080/", "height": 445}
matches = pd.read_csv("/content/sample_data/matches.csv")
matches.head()

# + id="nA3SyLi1U4zt" outputId="eb1dca85-04e4-4f16-98d4-a66b2a92c01c" colab={"base_uri": "https://localhost:8080/", "height": 394}
# setting up the table with relevant columns
dropList = ["result","player_of_match","venue","umpire1","umpire2","umpire3"]
matches.drop(labels=dropList, axis=1, inplace=True)
matches.head() 

# + id="Pd3l2_iBVlyA" outputId="a752893e-33b7-476e-95f4-9dcaa9d3922d" colab={"base_uri": "https://localhost:8080/", "height": 312}
matches[pd.isnull(matches['winner'])]

# + id="pGnAtkWHVomw" outputId="f9dde95f-c315-4b36-e01d-d1e6e61ac03f" colab={"base_uri": "https://localhost:8080/", "height": 49}
matches['winner'].fillna('Draw', inplace=True)
matches[pd.isnull(matches['winner'])]

# + id="N1xOYdgDVqfP" outputId="f4499d3b-a531-4eb3-b7b0-606cf554a7e0" colab={"base_uri": "https://localhost:8080/", "height": 490}
matches[pd.isnull(matches['city'])]

# + id="ZBg0kS0HVsKz" outputId="7a0502f5-12a7-43f5-ce21-8ab3053c17e7" colab={"base_uri": "https://localhost:8080/", "height": 49}
matches['city'].fillna('Dubai', inplace=True)
matches[pd.isnull(matches['city'])]

# + id="O98Q5N9zVuB8" outputId="b40ced7c-7c7a-49d9-f9e2-aca362ab8500" colab={"base_uri": "https://localhost:8080/"}
matches.replace(["Deccan Chargers","Delhi Daredevils"],["Sunrisers Hyderabad","Delhi Capitals"],inplace=True,regex=True)
match1 = matches[((matches.team1=="Royal Challengers Bangalore") & (matches.team2=="Sunrisers Hyderabad")) | ((matches.team1=="Sunrisers Hyderabad") & (matches.team2=="Royal Challengers Bangalore"))]
match1.shape[0]

# + id="9Sw-oxaJd7mB" outputId="4eaca2ee-30a6-4112-8bac-514f3d721b35" colab={"base_uri": "https://localhost:8080/"}
mw_srh = 0 
mw_rcb = 0
lst= [i for i in match1['winner']] 
print("Win Tracker!")
for i in lst:
  if i=="Royal Challengers Bangalore":
    mw_rcb += 1
  elif i=='Draw':
    continue
  else:
    mw_srh += 1
  print(str(mw_srh)+" "+str(mw_rcb))
print("SRH vs RCB : "+str(mw_srh)+" "+str(mw_rcb))

# + id="CIBO-yKZeZVb" outputId="357801ec-45a3-4ce5-dcc3-bc1655514857" colab={"base_uri": "https://localhost:8080/"}
last_3_season = match1[match1.season >= 2017]
last_3_season.groupby('winner').winner.count()


# + [markdown] id="JAgC7IJJWjqP"
# Out of 19 matches held between SRH and RCB , SRh leads RCB 14 is to 11. In the case with last three seasons, SRH has lead of victories over RR i.e 3 is to 2

# + id="FyY9pRGkaqWW"
def statistics_for_lists(lst):
  print("Maximum Value Of List:")
  print(max(lst))
  print("Median of the List:")
  print(statistics.median(lst))
  print("Mean of the List:")
  print(statistics.mean(lst))
  print("75% of the Median is:")
  print(statistics.median_high(lst))
  print("Minimum Value of List:")
  print(min(lst))


# + id="KonYIWemWeSX" outputId="0c7016df-f355-4187-b079-f7fd8b7ee186" colab={"base_uri": "https://localhost:8080/", "height": 394}
deliveries = pd.read_csv("/content/sample_data/deliveries.csv")
deliveries.head()

# + id="Xfj0J0pBWyRX" outputId="80999a81-4e76-4ec5-99a9-fbe345890a3d" colab={"base_uri": "https://localhost:8080/", "height": 394}
dropToBeList = ['inning','is_super_over','bye_runs','legbye_runs','fielder']
deliveries.drop(dropToBeList, axis=1, inplace=True)
deliveries.replace(['Deccan Chargers','Delhi Daredevils'],['Sunrisers Hyderabad','Delhi Capitals'],inplace=True,regex=True)
deliveries['dismissal_kind'].fillna('Not Out',inplace=True)
deliveries.head()

# + id="KlS2o3qIW2GL" outputId="f095efcf-900b-4d5e-c566-962de3fbddf8" colab={"base_uri": "https://localhost:8080/"}
ballbyball = deliveries[((deliveries.batting_team=="Royal Challengers Bangalore") & (deliveries.bowling_team=="Sunrisers Hyderabad")) | ((deliveries.batting_team=="Sunrisers Hyderabad") & (deliveries.bowling_team=="Royal Challengers Bangalore"))]
no_of_matches=list(set([i for i in ballbyball['match_id']]))
no_of_matches.sort()
print(len(no_of_matches))

# + id="bdWITQe5oNC3" outputId="4c5c2b15-ad52-4649-d38c-305c67886842" colab={"base_uri": "https://localhost:8080/"}
#Q4
wickets_lost_srh_pp = ballbyball[(ballbyball.batting_team=='Sunrisers Hyderabad') & (ballbyball.over>=1) & (ballbyball.over<=5)].groupby('match_id').player_dismissed.count()
wickets_lost_rcb_pp = ballbyball[(ballbyball.batting_team=='Royal Challengers Bangalore') & (ballbyball.over>=1) & (ballbyball.over<=5)].groupby('match_id').player_dismissed.count()
srh_pp=[i for i in wickets_lost_srh_pp]
rcb_pp=[i for i in wickets_lost_rcb_pp]
diff=[]
for i in range(len(srh_pp)):
  diff.append(abs(rcb_pp[i]-srh_pp[i]))
statistics_for_lists(diff)

# + id="1kJmZcQGBa-N" outputId="d57c0447-9f8d-449a-f22c-f444c132fede" colab={"base_uri": "https://localhost:8080/"}
#Q5
dot_balls = ballbyball[(ballbyball.total_runs==0)].groupby('match_id').total_runs.count()
dot_balls.describe()

# + [markdown] id="nFq6aDcCHC10"
# In all matches between RCB and SRH, the average number of dot balls expected is 81 to 88

# + id="itmnb6ZQsmut" outputId="893821a5-3543-4978-9d1c-577c3d0631d5" colab={"base_uri": "https://localhost:8080/"}
#Q2
total_scores = ballbyball.groupby('match_id').total_runs.sum()
total_scores.describe()

# + id="2lTPW4QcqWaX" outputId="642ce8dd-f97f-428f-a1d6-4f17ad27e1ca" colab={"base_uri": "https://localhost:8080/"}
#Q3
srh = ballbyball[ballbyball.batting_team=='Sunrisers Hyderabad']
srh_dif=[]
for i in no_of_matches:
  df = srh[srh.match_id==i]
  tot_runs = [k for k in df['total_runs']]
  wides = [k for k in df['wide_runs']]
  nobs = [k for k in df['noball_runs']]
  ball_to_30=0
  ball_to_50=0
  score_to_50=0
  score_to_30=0
  for j in range(len(tot_runs)):
    if(score_to_30 < 30 and wides[j]==0 and nobs[j]==0):
      ball_to_30 +=1
      ball_to_50 +=1
      score_to_30 += tot_runs[j]
      score_to_50 += tot_runs[j]
    elif(score_to_30 < 30 and (wides[j]!=0 or nobs[j]!=0)):
      score_to_30 += tot_runs[j]
      score_to_50 += tot_runs[j]
    elif(score_to_50 < 50 and wides[j]==0 and nobs[j]==0):
      score_to_50 += tot_runs[j]
      ball_to_50 += 1
    elif(score_to_50 < 50 and (wides[j]!=0 or nobs[j]!=0)):
      score_to_50 += tot_runs[j]
  diff = ball_to_50 - ball_to_30
  srh_dif.append(diff)
print(srh_dif)

# + id="Uc_qtyLqtDyZ" outputId="d6359093-cb19-4e09-d5fc-9e19bfa241f9" colab={"base_uri": "https://localhost:8080/"}
rcb = ballbyball[ballbyball.batting_team=='Royal Challengers Bangalore']
rcb_dif=[]
for i in no_of_matches:
  df = rcb[rcb.match_id==i]
  tot_runs = [k for k in df['total_runs']]
  wides = [k for k in df['wide_runs']]
  nobs = [k for k in df['noball_runs']]
  ball_to_30=0
  ball_to_50=0
  score_to_50=0
  score_to_30=0
  for j in range(len(tot_runs)):
    if(score_to_30 < 30 and wides[j]==0 and nobs[j]==0):
      ball_to_30 +=1
      ball_to_50 +=1
      score_to_30 += tot_runs[j]
      score_to_50 += tot_runs[j]
    elif(score_to_30 < 30 and (wides[j]!=0 or nobs[j]!=0)):
      score_to_30 += tot_runs[j]
      score_to_50 += tot_runs[j]
    elif(score_to_50 < 50 and wides[j]==0 and nobs[j]==0):
      score_to_50 += tot_runs[j]
      ball_to_50 += 1
    elif(score_to_50 < 50 and (wides[j]!=0 or nobs[j]!=0)):
      score_to_50 += tot_runs[j]
  diff = ball_to_50 - ball_to_30
  rcb_dif.append(diff)
print(rcb_dif)

# + id="3B9jN37vwXqk" outputId="fe3de085-1e90-4a94-baa1-0e9399668d2f" colab={"base_uri": "https://localhost:8080/"}
diff_bw_srh_rcb = []
for i in range(len(rcb_dif)):
  diff_bw_srh_rcb.append(abs(srh_dif[i]-rcb_dif[i]))
print(diff_bw_srh_rcb)
statistics_for_lists(diff_bw_srh_rcb)

# + id="UmCV74Kvw04A"


StarcoderdataJupyterScriptsDedupFiltered