IPL Analysis
A simple and brief analysis of the contibution of top batsmen for different IPL franchise from 2008-2020
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
df_ipl = pd.read_csv('C:/Users/sathyak/Desktop/ball-by-ball 2008-2020.csv')
df_ipl.head()
df_ipl.tail()
df_ipl.shape
df_ipl.groupby(["batting_team"])["total_runs"].sum().sort_values(ascending = False)
df_kkr = df_ipl[df_ipl["batting_team"]=='Kolkata Knight Riders']
df_kkr.head()
df_kkr.tail()
df_kkr.shape
kkr_totalruns = df_kkr['total_runs'].sum()
kkr_totalruns
kkr_batsman = pd.DataFrame((df_kkr.groupby(['batsman'])['batsman_runs'].sum()/kkr_totalruns)*100).sort_values(by='batsman_runs',ascending=False)
kkr_batsman
kkr_runs1=kkr_batsman[:14].copy()
kkr_runs1.reset_index(level=0, inplace=True)
kkr_runs2=pd.DataFrame(data={'batsman':['Others'],'batsman_runs':[kkr_batsman['batsman_runs'][14:].sum()]})
kkr_runs3 = pd.DataFrame(data={'batsman':['Extras'],'batsman_runs':((df_kkr['total_runs'] - df_kkr['batsman_runs']).sum()/kkr_totalruns)*100})
kkr_runs3
kkr_runs1.head()
kkr_runs2
kkr=pd.concat([kkr_runs1,kkr_runs2,kkr_runs3])
kkr.head()
kkr.tail()
kkr.shape
kkr=kkr.set_index('batsman')
kkr.head()
kkr.tail()
kkr
wp = { 'linewidth' : 2, 'edgecolor' : "white" }
colors = ['#9e8600','#a38b00','#a88f00','#b39800','#b89c00','#bda000','#c7a900','#ccad00','#d1b200','#dbba00','#e0bf00','#e6c300','#f0cc00','#f5d000','#947e00']
kkr.plot.pie(y='batsman_runs',wedgeprops = wp,figsize=(15,15),
startangle = 90,colors = colors,
textprops = dict(color ="Black"),autopct='%1.1f%%')
plt.tight_layout()
plt.show()
df_rr = df_ipl[df_ipl["batting_team"]=='Rajasthan Royals']
df_rr.head()
rr_totalruns = df_rr['total_runs'].sum()
rr_totalruns
rr_batsman = pd.DataFrame((df_rr.groupby(['batsman'])['batsman_runs'].sum()/rr_totalruns)*100).sort_values(by='batsman_runs',ascending=False)
rr_batsman
rr_runs1=rr_batsman[:14].copy()
rr_runs1.reset_index(level=0, inplace=True)
rr_runs2=pd.DataFrame(data={'batsman':['Others'],'batsman_runs':[rr_batsman['batsman_runs'][14:].sum()]})
rr_runs3 = pd.DataFrame(data={'batsman':['Extras'],'batsman_runs':((df_rr['total_runs'] - df_rr['batsman_runs']).sum()/rr_totalruns)*100})
rr_runs1.head()
rr_runs2.head()
rr=pd.concat([rr_runs1,rr_runs2,rr_runs3])
rr.head()
rr.tail()
rr=rr.set_index('batsman')
wp = { 'linewidth' : 2, 'edgecolor' : "white" }
colors = ['#9e8600','#a38b00','#a88f00','#b39800','#b89c00','#bda000','#c7a900','#ccad00','#d1b200','#dbba00','#e0bf00','#e6c300','#f0cc00','#f5d000','#947e00']
rr.plot.pie(y='batsman_runs',wedgeprops = wp,figsize=(15,15),
startangle = 90,colors = colors,
textprops = dict(color ="Black"),autopct='%1.1f%%')
plt.tight_layout()
plt.show()
df_csk = df_ipl[df_ipl["batting_team"]=='Chennai Super Kings']
df_csk.head()
df_csk.tail()
csk_totalruns = df_csk['total_runs'].sum()
csk_totalruns
csk_batsman = pd.DataFrame((df_csk.groupby(['batsman'])['batsman_runs'].sum()/csk_totalruns)*100).sort_values(by='batsman_runs',ascending=False)
csk_batsman.head()
csk_runs1=csk_batsman[:14].copy()
csk_runs1.reset_index(level=0, inplace=True)
csk_runs2=pd.DataFrame(data={'batsman':['Others'],'batsman_runs':[csk_batsman['batsman_runs'][14:].sum()]})
csk_runs3 = pd.DataFrame(data={'batsman':['Extras'],'batsman_runs':((df_csk['total_runs'] - df_csk['batsman_runs']).sum()/csk_totalruns)*100})
csk_runs1.head()
csk_runs2.head()
csk=pd.concat([csk_runs1,csk_runs2,csk_runs3])
csk.head()
csk.tail()
csk=csk.set_index('batsman')
wp = { 'linewidth' : 2, 'edgecolor' : "white" }
colors = ['#9e8600','#a38b00','#a88f00','#b39800','#b89c00','#bda000','#c7a900','#ccad00','#d1b200','#dbba00','#e0bf00','#e6c300','#f0cc00','#f5d000','#947e00']
csk.plot.pie(y='batsman_runs',wedgeprops = wp,figsize=(15,15),
startangle = 90,colors = colors,
textprops = dict(color ="Black"),autopct='%1.1f%%')
plt.tight_layout()
plt.show()
df_mi = df_ipl[df_ipl["batting_team"]=='Mumbai Indians']
df_mi.head()
df_mi.tail()
mi_totalruns = df_mi['total_runs'].sum()
mi_totalruns
mi_batsman = pd.DataFrame((df_mi.groupby(['batsman'])['batsman_runs'].sum()/mi_totalruns)*100).sort_values(by='batsman_runs',ascending=False)
mi_batsman.head(10)
mi_runs1=mi_batsman[:14].copy()
mi_runs1.reset_index(level=0, inplace=True)
mi_runs2=pd.DataFrame(data={'batsman':['Others'],'batsman_runs':[mi_batsman['batsman_runs'][14:].sum()]})
mi_runs3 = pd.DataFrame(data={'batsman':['Extras'],'batsman_runs':((df_mi['total_runs'] - df_mi['batsman_runs']).sum()/mi_totalruns)*100})
mi_runs1.head()
mi_runs2.head()
mi=pd.concat([mi_runs1,mi_runs2,mi_runs3])
mi.tail()
mi=mi.set_index('batsman')
wp = { 'linewidth' : 2, 'edgecolor' : "white" }
colors = ['#9e8600','#a38b00','#a88f00','#b39800','#b89c00','#bda000','#c7a900','#ccad00','#d1b200','#dbba00','#e0bf00','#e6c300','#f0cc00','#f5d000','#947e00']
mi.plot.pie(y='batsman_runs',wedgeprops = wp,figsize=(15,15),
startangle = 90,colors = colors,
textprops = dict(color ="Black"),autopct='%1.1f%%')
plt.tight_layout()
plt.show
df_rcb = df_ipl[df_ipl["batting_team"]=='Royal Challengers Bangalore']
df_rcb.head()
df_rcb.shape
rcb_totalruns = df_rcb['total_runs'].sum()
rcb_totalruns
rcb_batsman = pd.DataFrame((df_rcb.groupby(['batsman'])['batsman_runs'].sum()/rcb_totalruns)*100).sort_values(by='batsman_runs',ascending=False)
rcb_batsman.head()
rcb_runs1=rcb_batsman[:14].copy()
rcb_runs1.reset_index(level=0, inplace=True)
rcb_runs2=pd.DataFrame(data={'batsman':['Others'],'batsman_runs':[rcb_batsman['batsman_runs'][14:].sum()]})
rcb_runs3 = pd.DataFrame(data={'batsman':['Extras'],'batsman_runs':((df_rcb['total_runs'] - df_rcb['batsman_runs']).sum()/rcb_totalruns)*100})
rcb=pd.concat([rcb_runs1,rcb_runs2,rcb_runs3])
rcb.head()
rcb.tail()
rcb=rcb.set_index('batsman')
wp = { 'linewidth' : 2, 'edgecolor' : "white" }
colors = ['#9e8600','#a38b00','#a88f00','#b39800','#b89c00','#bda000','#c7a900','#ccad00','#d1b200','#dbba00','#e0bf00','#e6c300','#f0cc00','#f5d000','#947e00']
rcb.plot.pie(y='batsman_runs',wedgeprops = wp,figsize=(15,15),
startangle = 90,colors = colors,
textprops = dict(color ="Black"),autopct='%1.1f%%')
plt.tight_layout()
plt.show
df_kxip = df_ipl[df_ipl["batting_team"]=='Kings XI Punjab']
df_kxip.head()
kxip_totalruns = df_kxip['total_runs'].sum()
kxip_totalruns
kxip_batsman = pd.DataFrame((df_kxip.groupby(['batsman'])['batsman_runs'].sum()/kxip_totalruns)*100).sort_values(by='batsman_runs',ascending=False)
kxip_batsman.head()
kxip_runs1=kxip_batsman[:14].copy()
kxip_runs1.reset_index(level=0, inplace=True)
kxip_runs2=pd.DataFrame(data={'batsman':['Others'],'batsman_runs':[kxip_batsman['batsman_runs'][14:].sum()]})
kxip_runs3 = pd.DataFrame(data={'batsman':['Extras'],'batsman_runs':((df_kxip['total_runs'] - df_kxip['batsman_runs']).sum()/kxip_totalruns)*100})
kxip=pd.concat([kxip_runs1,kxip_runs2,kxip_runs3])
kxip.tail()
kxip=kxip.set_index('batsman')
wp = { 'linewidth' : 2, 'edgecolor' : "white" }
colors = ['#9e8600','#a38b00','#a88f00','#b39800','#b89c00','#bda000','#c7a900','#ccad00','#d1b200','#dbba00','#e0bf00','#e6c300','#f0cc00','#f5d000','#947e00']
kxip.plot.pie(y='batsman_runs',wedgeprops = wp,figsize=(15,15),
startangle = 90,colors = colors,
textprops = dict(color ="Black"),autopct='%1.1f%%')
plt.tight_layout()
plt.show
df_dc = df_ipl[df_ipl["batting_team"]=='Delhi Daredevils'|'Delhi Capitals']
df_dc = df_ipl[df_ipl['batting_team'].isin(['Delhi Daredevils', 'Delhi Capitals']) ]
df_dc
dc_totalruns = df_dc['total_runs'].sum()
dc_totalruns
dc_batsman = pd.DataFrame((df_dc.groupby(['batsman'])['batsman_runs'].sum()/dc_totalruns)*100).sort_values(by='batsman_runs',ascending=False)
dc_batsman
dc_runs1=dc_batsman[:14].copy()
dc_runs1.reset_index(level=0, inplace=True)
dc_runs2=pd.DataFrame(data={'batsman':['Others'],'batsman_runs':[dc_batsman['batsman_runs'][14:].sum()]})
dc_runs3 = pd.DataFrame(data={'batsman':['Extras'],'batsman_runs':((df_dc['total_runs'] - df_dc['batsman_runs']).sum()/dc_totalruns)*100})
dc=pd.concat([dc_runs1,dc_runs2,dc_runs3])
dc.head()
dc=dc.set_index('batsman')
wp = { 'linewidth' : 2, 'edgecolor' : "white" }
colors = ['#9e8600','#a38b00','#a88f00','#b39800','#b89c00','#bda000','#c7a900','#ccad00','#d1b200','#dbba00','#e0bf00','#e6c300','#f0cc00','#f5d000','#947e00']
dc.plot.pie(y='batsman_runs',wedgeprops = wp,figsize=(15,15),
startangle = 90,colors = colors,
textprops = dict(color ="Black"),autopct='%1.1f%%')
plt.tight_layout()
plt.show
df_ipl.groupby(["batting_team"])["total_runs"].sum().sort_values(ascending = False)
df_srh = df_ipl[df_ipl['batting_team'].isin(['Sunrisers Hyderabad', 'Deccan Chargers']) ]
df_srh
srh_totalruns = df_srh['total_runs'].sum()
srh_batsman = pd.DataFrame((df_srh.groupby(['batsman'])['batsman_runs'].sum()/srh_totalruns)*100).sort_values(by='batsman_runs',ascending=False)
srh_batsman
srh_runs1=srh_batsman[:14].copy()
srh_runs1.reset_index(level=0, inplace=True)
srh_runs2=pd.DataFrame(data={'batsman':['Others'],'batsman_runs':[srh_batsman['batsman_runs'][14:].sum()]})
srh_runs3 = pd.DataFrame(data={'batsman':['Extras'],'batsman_runs':((df_srh['total_runs'] - df_srh['batsman_runs']).sum()/srh_totalruns)*100})
srh=pd.concat([srh_runs1,srh_runs2,srh_runs3])
srh=srh.set_index('batsman')
wp = { 'linewidth' : 2, 'edgecolor' : "white" }
colors = ['#9e8600','#a38b00','#a88f00','#b39800','#b89c00','#bda000','#c7a900','#ccad00','#d1b200','#dbba00','#e0bf00','#e6c300','#f0cc00','#f5d000','#947e00']
srh.plot.pie(y='batsman_runs',wedgeprops = wp,figsize=(15,15),
startangle = 90,colors = colors,
textprops = dict(color ="Black"),autopct='%1.1f%%')
plt.tight_layout()
plt.show