<jupyter_start><jupyter_text>Clean the BATSMAN file- Remove the Unnamed column- Remove entries with DNB,TDNB and absent- Reseting Inex: Making Index values continuos- Replace '*' with space, to convert the column to numbers<jupyter_code>df.drop(labels="Unnamed: 0",axis=1,inplace=True) #Remove the Unnamed column
df.head(5)
a=[]
# Remove entries with DNB,TDNB and absent
for i in range(1,len(df)):
    if(df.Runs[i]=="DNB" or df.Runs[i]=="TDNB" or df.Runs[i]=="absent"):
        a.append(i)
        
a
# Reseting Inex: Making Index values continuos
df.drop(a,inplace=True)
df.reset_index(drop=True, inplace=True)
df.head(5)
df["Runs"] = df["Runs"].str.replace(r'*', '')
# Replace '*' with space, to convert the column to numbers
df.head(5)<jupyter_output><empty_output><jupyter_text>Clean BATSMAN Function<jupyter_code>def clean_batsman(file):
    fe=f"data/{file}.csv"
    df=pd.read_csv(fe)
    df.drop(labels="Unnamed: 0",axis=1,inplace=True) #Remove the Unnamed column
    a=[]
    # Remove entries with DNB,TDNB and absent
    for i in range(0,len(df)):
        if( df["4s"][i] =='-'):
            df["4s"][i] = 0
        if( df["6s"][i] =='-'):
            df["6s"][i] = 0
        if(df.Runs[i]=="DNB" or df.Runs[i]=="TDNB" or df.Runs[i]=="absent"):
            a.append(i)
    df.drop(a,inplace=True)
    df.reset_index(drop=True, inplace=True)
    df["Runs"] = df["Runs"].str.replace(r'*', '')
    # Replace '*' with space, to convert the column to numbers
    return(df)
df=clean_batsman("kohli1")
df
print(clean_batsman("kohli1"))<jupyter_output>Runs Mins  BF  4s 6s      SR Pos Dismissal  Inns      Opposition  \
0    26   30  21   3  1  123.80   5   not out     2      v Zimbabwe   
1    28   28  19   5  0  147.36   1    bowled     1  v South Africa   
2    14   15  12   2  0  116.66   3    caught     1   v West Indies   
3     4    5   5   0  0   80.00   4    caught     1       v England   
4    15   21  16   3  0   93.75   3    caught     1       v England   
5    22   30  21   0  1  104.76   3    caught     2     v Australia   
6    31   29  24   3  0  129.16   3    caught     2     v Australia   
7    68   65  48  11  1  141.66   3    caught     1     v Sri Lanka   
8    70   67  41  10  1  170.73   2    caught     2   v New Zealand   
9    50   60  39   4  2  128.20   3    caught     1   v Afghanistan   
10   40   33  32   6  0  125.00   3    caught     1       v England   
11   15   18  13   2  0  115.38   3    caught     1     v Australia   
12   78   61  61   8  2  127.86   3   not out     2      v Pakistan   
13    [...]<jupyter_text>Number of 4s, 6s- Count the number of 4s, 6s against any oponent- Count 4s, 6s in any ground<jupyter_code>print(df["4s"].astype(int).sum())
print(df["6s"].astype(int).sum())
# Number of 4s and 6s filtered via opponent
o=input(str("Enter opposition: "))

opp="v "+o
s4=0
s6=0
f=0
for i in range(0, len(df)):
    if(df["Opposition"][i]==opp):
        f=1
        s4=s4+int(df["4s"][i])
        s6=s6+int(df["6s"][i])

if(f!=0):
    print(s4,s6)
else:
    print(f"No Opposition named '{o}' found.")
# Number of 4s and 6s filtered via ground
gnd=input(str("Enter name of Ground: "))

s4=0
s6=0
f=0
for i in range(0, len(df)):
    if(df["Ground"][i]==gnd):
        f=1
        s4=s4+int(df["4s"][i])
        s6=s6+int(df["6s"][i])

if(f!=0):
    print(s4,s6)
else:
    print(f"No Ground named '{gnd}' found.")<jupyter_output>Enter name of Ground: Kolkata
10 1<jupyter_text>Average runs w.r.t. Ground<jupyter_code>gnd=input(str("Enter name of Ground: "))

ru=0
sr=0
n=0
for i in range(0, len(df)):
    if(df["Ground"][i]==gnd):
        f=1
        ru=ru+int(df["Runs"][i])
        sr=sr+float(df["SR"][i])
        n=n+1

if(f!=0):
    print(f"Runs scored at {gnd}= {ru}")
    if(n!=0):
        print(f"Number of matches played at {gnd}= {n}")
        print(f"Average Runs scored at {gnd}= {int(ru/n)}")
        print(f"Strike Rate of the player = {round(sr/n,2)}")
else:
    print(f"No Ground named '{gnd}' found.")
o=input(str("Enter opposition: "))
f=0
opp="v "+o
ru=0
n=0
sr=0
for i in range(0, len(df)):
    if(df["Opposition"][i]==opp):
        f=1
        ru=ru+int(df["Runs"][i])
        sr=sr+float(df["SR"][i])
        n=n+1

if(f!=0):
    print(f"Runs scored against {o}= {ru}")
    print(f"Average Runs scored against {o}= {int(ru/n)}")
    print(f"Strike Rate of the player = {round(sr/n,2)}")
else:
    print(f"No Opposition named '{o}' found.")<jupyter_output>Enter opposition: Kolkata
No Opposition named 'Kolkata' found.<jupyter_text>Player's Total Stats<jupyter_code>ru=0
dis=0
sr=0
for i in range(0, len(df)):
    ru=ru+int(df["Runs"][i])
    sr=sr+float(df["SR"][i])
    if(df["Dismissal"][i]!="not out"):
        dis=dis+1
print(f"Total Runs scored by the player = {ru}")
print(f"Total Average of the player = {int(ru/len(df))}")
print(f"Number of matches played = {len(df)}")
print(f"Total Strike Rate of the player = {round(sr/len(df),2)}")
print(f"Number of times dismissed= {dis}")
df=clean_batsman("kohli")
def dict_ground():
    li=list(set(df["Ground"]))
    dict_gnd={}
    for x in li:
        ru=0
        s4=0
        s6=0
        for i in range(0, len(df)):
            if(df["Ground"][i]==x):
                ru=ru+int(df["Runs"][i])
                s4=s4+int(df["4s"][i])
                s6=s6+int(df["6s"][i])
        val=[ru,s4,s6]
        dict_gnd[x]=val
    print(dict_gnd)
dict_ground()
df=clean_batsman("sehwag")
def dict_opposition():
    li=list(set(df["Opposition"]))
    dict_opp={}
    for x in li:
        ru=0
        s4=0
        s6=0
        for i in range(0, len(df)):
            if(df["Opposition"][i]==x):
                ru=ru+int(df["Runs"][i])
                s4=s4+int(df["4s"][i])
                s6=s6+int(df["6s"][i])
        val=[ru,s4,s6]
        dict_opp[x[2:]]=val
    print(dict_opp)
dict_opposition()<jupyter_output>{'South Africa': [105, 13, 2], 'West Indies': [290, 34, 4], 'New Zealand': [544, 62, 15], 'England': [167, 24, 2], 'Pakistan': [404, 59, 9], 'Sri Lanka': [716, 98, 5], 'Bangladesh': [474, 59, 11], 'Australia': [199, 27, 2], 'Zimbabwe': [50, 8, 0]}<jupyter_text>Preparing dict of batsman with all data<jupyter_code>batsman={'devilliers',
 'ganguly',
 'gayle',
 'kohli',
 'kohli1',
 'maxwell',
 'sehwag',
 'sehwag1',
 'sehwag2',
 'tendulkar',
 'tendulkar1',
 'tendulkar2',
 'tendulkarsp'}

bowlers={'ashwin',
 'ashwin1',
 'badree',
 'kumble',
'mitchell',
 'kumble1',
 'kumblesp',
 'malinga',
 'malinga1','mendis','murali',
 'narine','southee',
 'steyn','warne'}
pd.set_option('display.max_rows',1000)
x=clean_batsman("sehwag")
x
def dict_opposition_file(df):
    li=list(set(df["Opposition"]))
    dict_opp={}
    for x in li:
        ru=0
        s4=0
        s6=0
        for i in range(0, len(df)):
            if(df["Opposition"][i]==x):
                ru=ru+int(df["Runs"][i])
                s4=s4+int(df["4s"][i])
                s6=s6+int(df["6s"][i])
        val=[ru,s4,s6]
        dict_opp[x[2:]]=val
    return(dict_opp)

def dict_ground_file(df):
    li=list(set(df["Ground"]))
    dict_gnd={}
    for x in li:
        ru=0
        s4=0
        s6=0
        for i in range(0, len(df)):
            if(df["Ground"][i]==x):
                ru=ru+int(df["Runs"][i])
                s4=s4+int(df["4s"][i])
                s6=s6+int(df["6s"][i])
        val=[ru,s4,s6]
        dict_gnd[x]=val
    return(dict_gnd)
df=clean_batsman("kohli")
dict_ground_file(df)
val1=dict_opposition_file(df)
print(val1.values())
def ground_details(l,df):
        ru=0
        s4=0
        s6=0
        cnt=0
        sr=0
        bf=0
        for i in range(0, len(df)):
            if(df["Ground"][i]==l):
                ru=ru+int(df["Runs"][i])
                s4=s4+int(df["4s"][i])
                s6=s6+int(df["6s"][i])
                cnt=cnt+1
                sr=sr+float(df["SR"][i])
                bf=bf+int(df["BF"][i])
        return(ru,s4,s6,cnt,round(sr/cnt,2),bf)
x,y,z,a,b,c=ground_details(l="Kolkata",df=df)
print(x,y,z,a,b,c)
def prep_dataset_ground(batsman):
    ndf=pd.DataFrame(columns=["Batsman","Ground","Runs","4s","6s","No. of Matches Played","Average Strike Rate","Balls Faced"])
    
    for bat in batsman:
        #print(str(x))
        df=clean_batsman(str(bat))
        li=list(set(df["Ground"]))
        for gnd in li:
            ru,s_4,s_6,nmf,avg_sr,bf=ground_details(gnd,df)
            add={"Batsman":bat,"Ground":gnd,"Runs":ru,"4s":s_4,"6s":s_6,"No. of Matches Played":nmf,"Average Strike Rate":avg_sr,"Balls Faced":bf}
            ndf=ndf.append(add,ignore_index=True)
    return(ndf)        
            
gnd_df=prep_dataset_ground(batsman)
gnd_df
def opposition_details(l,df):
        ru=0
        s4=0
        s6=0
        cnt=0
        sr=0
        bf=0
        for i in range(0, len(df)):
            if(df["Opposition"][i]==l):
                ru=ru+int(df["Runs"][i])
                s4=s4+int(df["4s"][i])
                s6=s6+int(df["6s"][i])
                cnt=cnt+1
                sr=sr+float(df["SR"][i])
                bf=bf+int(df["BF"][i])
        return(ru,s4,s6,cnt,round(sr/cnt,2),bf)
def prep_dataset_opposition(batsman):
    ndf=pd.DataFrame(columns=["Batsman","Ground","Runs","4s","6s","No. of Matches Played","Average Strike Rate","Balls Faced"])
    
    for bat in batsman:
        #print(str(x))
        df=clean_batsman(str(bat))
        li=list(set(df["Opposition"]))
        for gnd in li:
            ru,s_4,s_6,nmf,avg_sr,bf=opposition_details(gnd,df)
            add={"Batsman":bat,"Ground":gnd,"Runs":ru,"4s":s_4,"6s":s_6,"No. of Matches Played":nmf,"Average Strike Rate":avg_sr,"Balls Faced":bf}
            ndf=ndf.append(add,ignore_index=True)
    return(ndf)        
            
opp_df=prep_dataset_opposition(batsman)
opp_df
def prep_all(batsman):
    ndf=pd.DataFrame(columns=["Batsman","Opposition","Runs","4s","6s"])
    
    for bat in batsman:
        #print(str(x))
        df=clean_batsman(str(bat))
        # This much is working
        val1=dict_opposition_file(df)
        for opp in val1:
            print(x)
        add={"Batsman":bat,"Opposition":opp,"Runs":ru,"4s":s_4,"6s":s_6}   
        ndf=ndf.append(add,ignore_index=True)
        val1=dict_opposition_file(df)
        val2=dict_ground_file(df)
        batsman_opp_dict[x]=val1
        batsman_gnd_dict[x]=val2
    return(batsman_opp_dict,batsman_gnd_dict)
        
prep_all(batsman)
print(clean_batsman("kohli"))
x = clean_batsman("kohli")
x
prep_all(batsman)
def dict_ground_file(file):
    df=pd.read_csv(file)
    li=list(set(df["Ground"]))
    dict_gnd={}
    for x in li:
        ru=0
        s4=0
        s6=0
        for i in range(0, len(df)):
            if(df["Ground"][i]==x):
                ru=ru+int(df["Runs"][i])
                s4=s4+int(df["4s"][i])
                s6=s6+int(df["6s"][i])
        val=[ru,s4,s6]
        dict_gnd[x]=val
    print(dict_gnd)
x="kohli"
file=f"data/{x}.csv"
df=pd.read_csv(file)
df.head(5)<jupyter_output><empty_output><jupyter_text>Cleaning Bowler's File<jupyter_code>df=pd.read_csv("data/malinga.csv")
df.drop(labels="Unnamed: 0",axis=1,inplace=True)
df["Bowls_Bowled"]=df["Overs"]*6
df["Bowls_Bowled"]=df["Bowls_Bowled"].astype(int)
df.head(5)
o=input(str("Enter opposition: "))

opp="v "+o
md=0
ec=0
k=0
f=0
for i in range(0, len(df)):
    if(df["Opposition"][i]==opp):
        f=1
        md+=int(df["Mdns"][i])
        ec+=int(df["Econ"][i])
        k=k+1
        
if(f!=0):
    print(f"Average Maiden Overs against the opposition= {int(md/k)},\nAverage Economy= {int(ec/k)} ")
else:
    print(f"No Opposition named '{o}' found.")
# Number of 4s and 6s filtered via ground
gnd=input(str("Enter name of Ground: "))

md=0
ec=0
k=0
f=0

for i in range(0, len(df)):
    if(df["Ground"][i]==gnd):
        f=1
        md+=int(df["Mdns"][i])
        ec+=int(df["Econ"][i])
        k=k+1

if(f!=0):
    print(f"Average Maiden Overs at the ground= {int(md/k)},\nAverage Economy= {int(ec/k)} ")
else:
    print(f"No Ground named '{gnd}' found.")
md=0
bb=0
ec=0
ru=0
wt=0
for i in range(0, len(df)):
    md+=int(df["Mdns"][i])
    ec+=int(df["Econ"][i])
    bb+=int(df["Bowls_Bowled"][i])
    ru+=int(df["Runs"][i])
    wt+=int(df["Wkts"][i])
    

print(f"Average Maiden Overs at the ground= {int(md/len(df))},\nAverage Economy= {int(ec/len(df))} ")
print(f"Total Balls Bowled= {int(bb)},\nAverage Runs = {int(ru/len(df))} ")
print(f"Total Wickets taken = {int(wt)},\nTotal Matches played= {int(len(df))} ")<jupyter_output>Average Maiden Overs at the ground= 1,
Average Economy= 3 
Total Balls Bowled= 5195,
Average Runs = 56 
Total Wickets taken = 101,
Total Matches played= 59<jupyter_text>Expected type of match column- Trying to guess from the bowls bowled: which match was it<jupyter_code>df["ExType_match"]="-"
for i in range(0,len(df)):
    if(df["Inns"][i]>2 or df["Overs"][i]>10):
        #TEST
        df["ExType_match"][i]="Test"
    elif(df["Overs"][i]<=4):
        #T20
        df["ExType_match"][i]="T20"
    elif(df["Overs"][i]>4 and df["Overs"][i]<=10):
        #ODI
        df["ExType_match"][i]="ODI"
df["ExType_match"]<jupyter_output><empty_output><jupyter_text>Preparing the list of players we have data for.<jupyter_code>import os
names=os.listdir("data")
names
name = {x.replace('.csv', '') for x in names}
name
batsman={'devilliers',
 'ganguly',
 'gayle',
 'kohli',
 'kohli1',
 'maxwell', 
 'sehwag',
 'sehwag1',
 'sehwag2',
 'tendulkar',
 'tendulkar1',
 'tendulkar2',
 'tendulkarsp'}

bowlers={'ashwin',
 'ashwin1',
 'badree',
 'kumble',
         'mitchell',
 'kumble1',
 'kumblesp',
 'malinga',
 'malinga1','mendis','murali',
 'narine','southee',
 'steyn','warne'}
len(name)
len(batsman)+len(bowlers)
# To check if I not repeated any values
set(batsman).intersection(bowlers)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18324493206423018, 'fraction_numerical': 0.04127007193199157, 'mean_word_length': 2.9135626954791016, 'pattern_counts': {'":': 21, '<': 34, '<?xml version=': 0, '>': 35, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '342550', 'license': 'MIT', 'n_tokens_mistral': 6043, 'n_tokens_neox': 5732, 'n_words': 1105, 'path': 'Cricket.ipynb', 'repo_name': 'NamanDhamejaBITS/Cricket_dataset', 'size': '13763'}