You are on page 1of 5
‘wraor2020 Test - Jupyter Notebook Test4 Note: Do not rename the notebook Honour Code: By taking the test, you undertake to abide by the following in letter and spirit: | understand that this is an open book test and | am allowed to refer to class notes and documentation. | agree to not receiving any help from others and not giving help to others during the test. Read the instructions carefull 1) Enter your code where indicated: i.e. enter just below the comment line # YOUR CODE HERE. You will not be able to enter in other places. 2) Do NOT delete or modify any line of code. For e.g. the line raise NotimplementedError(). Doing so may result in your fle not being evaluated. 3) If your function does not do exactly what is asked, it will be marked as incorrect. Note whether the question asks the function to print or return. 4) In the rare case that you don't have the module ‘nose’ installed, uncomment and run the cell below: pip install nose In [1]: pip install nose Requirement already satisfied: nose in kages (1.3.7) Note: you may need to restart the kernel to use updated packages. : \users\nitro\anaconda3\1ib\site-pac In [2]: import numpy as np import pandas as pd Read the dataset The dataset is about cricket matches. Run the line below to read the dataset, localhost 8888/notebooks/Test4.ipynb 48 ‘wraor2020 Test - Jupyter Notebook In [4]: df = pd.read_csv(‘cricket.csv') df.head() out [4]: id season city dato team1_—team2_toss_winner toss_decision result + Royal Royal © 1 2017 Hyderabad 2017 Suntisers Chatiengers Challengers field normal 0405 Hyderabad "Bangalore Bangalore 2017- Mumbai Rising Pune Rising Pune 12 2017 Pune 4.08 Indians Supergiant —Supergiant field normal Kolkata Kolkata 2017- Gujarat K 2 3 2017 Rajkot iar Koight Knight field normal 407 ons: Riders Riders indore 2017- Rising Pune — Kings XI_——Kings X! orm 3 4 2017 Indore 94.08 Supergiant Punjab Punjab Feld normal Royal Royal 2017- Dethi 4 5 2017 Bangalore Giigp Shalergers paredouis Challengers bat normal Warm-Up. Time for some stretches before you enter the ground Check out the first 5 rows of the dataset Note the index of the data frame Also note the names of each column Important: Your function must retum exactly what is asked. Please read carefully if the question asks to retumn a dataframe or list or string or integer etc Question 1 Write a function that returns the number of rows alone as an integer In [ J: def numrows (df): num rows # YOUR CODE HERE return (len(d#)) raise NotInplementedError() localhost 8888/notebooks/Test4.ipynb ‘wraor2020 Test - Jupyter Notebook In]: Question 2 Write a function that takes the argument N where N stands for the data frame's index and returns the value of team/ in that match as a string In]: def teamiNthIndex(N): team given index # YOUR CODE HERE return (df.loc(N]["team1"]) raise NotInplenentedérror() In [ ]: Question 3 Write a function that takes the argument N where N stands for the data frame's index and returns the winner in that match In [ J def winnerNthIndex(N): winner given index # YOUR CODE HERE return (df.loc[N]["“winner"}) raise NotInplementedError() In[ ]: Question 4 Write a function that takes the argument N where N stands for the match id (and not the dataframe index) and returns the winner in that match as a string localhost 8888/notebooks/Test4.ipynb ‘wraor2020 Test - Jupyter Notebook In[ J: def winnernthMatch(N): winner given match id # YOUR CODE HERE return (df[df["id"]== NJ["winner"]) raise NotInplementedérror() In []: Question 5 Write a function that finds the number of calendar years (seasons) for which matches were played and returns it as an integer. In [ J: def numyears(dF): number of years matches were played # YOUR CODE HERE return (len(df["season"].unique())) raise NotInplementedError() In[]: Question 6 Write a function that finds the number of matches played in a city given by the user and returns an integer. In [ J: def matchesinCity(city): number of matches in a city # YOUR CODE HERE return (len(df[df["city"] raise NotInplenentederror() city])) In [ J: Question 7 localhost 8888/notebooks/Test4.ipynb ais, ‘wraor2020 Test - Jupyter Notebook Write a function that finds the number of matches where the match winner was also the toss winner and returns an integer, In [ J: def tossAndmatchwon(d#): num matches where toss winner = match winner # YOUR CODE HERE return (len(df[df["toss_winner"] raise NotInplementederror() dF["winner"]])) In [ J: Question 8 Write a function that finds the average of win_by_runs in matches in a user given season (i.e. year) and returns an integer. In [ ]: def AvgInSeason(year): avg of runs of won by in a year # YOUR CODE HERE return (df[df["season"]== year] ["win_by_runs"].mean()) raise NotImplementedError() In []: localhost 8888/notebooks/Test4.ipynb

You might also like