Task 1:
ipl18['Points']=ipl18['Won']*2 +ipl18['Lost']*0+ ipl18['N/R']*1+ ipl18['Tied']*1
ipl18
Output:
Team | Matches | Won | Lost | Tied | N/R | NRR | For | Against | Points | |
---|---|---|---|---|---|---|---|---|---|---|
1 | SRH | 14 | 9 | 5 | 0 | 0 | 0.284 | 2230 | 2193 | 18 |
2 | CSK | 14 | 9 | 5 | 0 | 0 | 0.253 | 2488 | 2433 | 18 |
3 | KKR | 14 | 8 | 6 | 0 | 0 | -0.070 | 2363 | 2425 | 16 |
4 | RR | 14 | 7 | 7 | 0 | 0 | -0.250 | 2130 | 2141 | 14 |
5 | MI | 14 | 6 | 8 | 0 | 0 | 0.317 | 2380 | 2282 | 12 |
6 | RCB | 14 | 6 | 8 | 0 | 0 | 0.129 | 2322 | 2383 | 12 |
7 | KXIP | 14 | 6 | 8 | 0 | 0 | -0.502 | 2210 | 2259 | 12 |
8 | DD | 14 | 5 | 9 | 0 | 0 | -0.222 | 2297 | 2304 | 10 |
ipl17['Points']=ipl17['Won']*2 +ipl17['Lost']*0+ ipl17['N/R']*1+ ipl17['Tied']*1
ipl17
Output:
Team | Matches | Won | Lost | Tied | N/R | NRR | For | Against | Points | |
---|---|---|---|---|---|---|---|---|---|---|
1 | MI | 14 | 10 | 4 | 0 | 0 | 0.784 | 2407 | 2242 | 20 |
2 | RPS | 14 | 9 | 5 | 0 | 0 | 0.176 | 2180 | 2165 | 18 |
3 | SRH | 14 | 8 | 5 | 0 | 1 | 0.469 | 2221 | 2118 | 17 |
4 | KKR | 14 | 8 | 6 | 0 | 0 | 0.641 | 2329 | 2300 | 16 |
5 | KXIP | 14 | 7 | 7 | 0 | 0 | 0.123 | 2207 | 2229 | 14 |
6 | DD | 14 | 6 | 8 | 0 | 0 | -0.512 | 2219 | 2255 | 12 |
7 | GL | 14 | 4 | 10 | 0 | 0 | -0.412 | 2406 | 2472 | 8 |
8 | RCB | 14 | 3 | 10 | 0 | 1 | -1.299 | 1845 | 2033 | 7 |
Extract Top Four Teams:
q1 = ipl18.loc [0:4, ['Team', 'Points']]
print(q1)
Output:
Team Points 1 SRH 18 2 CSK 18 3 KKR 16 4 RR 14
Filtering based on conditions:
q2 = ipl18[(ipl18["NRR"]>0) & (ipl18["For"] > ipl18["Against"])]
Output:
Team | Matches | Won | Lost | Tied | N/R | NRR | For | Against | Points | |
---|---|---|---|---|---|---|---|---|---|---|
1 | SRH | 14 | 9 | 5 | 0 | 0 | 0.284 | 2230 | 2193 | 18 |
2 | CSK | 14 | 9 | 5 | 0 | 0 | 0.253 | 2488 | 2433 | 18 |
5 | MI | 14 | 6 | 8 | 0 | 0 | 0.317 | 2380 | 2282 | 12 |
Operations on multiple dataframes:
# append data of 2 years
a = ipl17.append(ipl18)
# sum won,lost and points by team
b = a[["Team","Won","Lost","Points"]].groupby(["Team"]).sum()
# sort by number of won
b.sort_values(by=["Won"], inplace=True, ascending=False)
# Team with greater than 25 points
c = b[b["Points"]>25]
# calculate % won
q3 = (c["Won"] / (c["Won"] + c["Lost"]))*100
print(q3)
Output:
Team SRH 62.962963 KKR 57.142857 MI 57.142857 KXIP 46.428571 dtype: float64
Hi guys, Can you please help me to solve below problem.
Practice Exercise 1
This is the first exercise for Pandas. You are provided with 2 fictional dataframes based on Indian Premier League (IPL). The first dataframe contains the data for all the teams who participated in the year 2018. In the second dataframe, the data is for the year 2017.
In [1]:
Data Dictionary
In [2]:
In [3]:
Out[3]:
In [11]:
Out[11]:
In [12]:
Out[12]:
In [24]:
Out[24]:
In [5]:
In [6]:
Out[6]:
In [35]:
Out[35]:
In [56]:
As a part of this exercise, solve the questions that are provided below. There are few tasks that you will have to perform to be able to answer the questions.
Task 1
You have to create a new column 'Points' in both the dataframes that stores the total points scored by each team. The following scoring system is used to calculate the points of a team:
In [ ]:
Q1: Extract Top Four Teams
Which of the following commands can you use to extract the top 4 teams in the dataset ‘ipl18’ with just the ‘Team’ and ‘Points’ column?
In [ ]:
Q2: Filtering based on conditions
Suppose in ‘ipl18’, you want to filter out the teams that have an NRR greater than zero, and for which the ‘For’ score exceeds the ‘Against’ score, i.e. both the conditions should be satisfied. Which teams will be left after you perform the above filtration? (Run the commands on the Python Notebook provided, rather than performing a manual calculation)
In [ ]:
Q3: Operations on multiple dataframes
If all the stats are taken for both ‘ipl17’ and ‘ipl18’, which team with its total points greater than 25 will have the highest win percentage?