Practice and reinforce the concepts from Lesson 15
:computer: Activity Type: Coding Exercise
⏱️ Estimated Time: 30-40 minutes
import numpy as np
import pandas as pd
:bulb: Always import pandas as
pd
and numpy asnp
- this is the standard convention that makes your code easier for others to read!
Exercise Steps
Step One: Create the Data ⏱️ (3 minutes)
- Copy and paste the data below into your notebook:
python
data = [ ["Jane",10,9,10,29], ["Mark",9,9,9,27], ["Bill",7,10,9,26], ["Jack",8,9,7,24], ["Ray",8,9,6,23], ["Harry",5,5,10,20], ["Louis",6,6,5,17], ["Prince",5,4,6,15], ] columns = ["Player", "Round 1", "Round 2", "Round 3", "Total"] index = ["1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th"]
Step 2: Create the DataFrame ⏱️ (5 minutes)
- Create a Pandas DataFrame named df using the
DataFrame()
function- Use the data, columns, and index variables you created in Step 1
- Your DataFrame should look like this:
Expected output:
textPlayer Round 1 Round 2 Round 3 Total 1st Jane 10 9 10 29 2nd Mark 9 9 9 27 3rd Bill 7 10 9 26 4th Jack 8 9 7 24 5th Ray 8 9 6 23 6th Harry 5 5 10 20 7th Louis 6 6 5 17 8th Prince 5 4 6 15
Step 3: Explore DataFrame Information ⏱️ (5 minutes)
- Display the column names using
df.columns
- Display the row index using
df.index
- Show the first 5 rows using
df.head()
- Display detailed information using
df.info()
:bulb: Tip The
info()
method is very useful - it shows you the data type of each column and whether there are any missing values!
Expected outputs:
Index(['Player', 'Round 1', 'Round 2', 'Round 3', 'Total'], dtype='object')
Index(['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th'], dtype='object')
Player Round 1 Round 2 Round 3 Total
1st Jane 10 9 10 29
2nd Mark 9 9 9 27
3rd Bill 7 10 9 26
4th Jack 8 9 7 24
5th Ray 8 9 6 23
<class 'pandas.core.frame.DataFrame'>
Index: 8 entries, 1st to 8th
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Player 8 non-null object
1 Round 1 8 non-null int64
2 Round 2 8 non-null int64
3 Round 3 8 non-null int64
4 Total 8 non-null int64
dtypes: int64(4), object(1)
memory usage: 384.0+ bytes
df['Round 2']
Expected output:
1st 9
2nd 9
3rd 10
4th 9
5th 9
6th 5
7th 6
8th 4
Name: Round 2, dtype: int64
df[['Player', 'Total']]
Expected output:
Player Total
1st Jane 29
2nd Mark 27
3rd Bill 26
4th Jack 24
5th Ray 23
6th Harry 20
7th Louis 17
8th Prince 15
loc[]
to access the data for the 5th ranked playerdf.loc['5th']
:bulb: Tip
loc[]
uses the actual index labels (like '1st', '2nd', etc.), not numbers!
Expected output:
Player Ray
Round 1 8
Round 2 9
Round 3 6
Total 23
Name: 5th, dtype: object
iloc[]
to access rows for 2nd to 5th ranked playersiloc[]
uses integer positions (0-based indexing)Expected output:
Player Round 1 Round 2 Round 3 Total
2nd Mark 9 9 9 27
3rd Bill 7 10 9 26
4th Jack 8 9 7 24
5th Ray 8 9 6 23
sum()
method on the 'Total' columndf['Total'].sum()
Expected Answer: 181
max()
method on the 'Round 3' columnExpected Answer: 10
describe()
Expected output:
Round 1 Round 2 Round 3 Total
count 8.000000 8.000000 8.000000 8.000000
mean 7.250000 7.625000 7.750000 22.625000
std 1.832251 2.263846 1.982062 4.926242
min 5.000000 4.000000 5.000000 15.000000
25% 5.750000 5.750000 6.000000 19.250000
50% 7.500000 9.000000 8.000000 23.500000
75% 8.250000 9.000000 9.250000 26.250000
max 10.000000 10.000000 10.000000 29.000000
df
or printing itloc[]
uses labels (like '5th')iloc[]
uses integer positions (like 4 for the 5th item):warning: Warning Important: Make sure to review all your answers before submitting!
- Check that all outputs match the expected results
- Ensure your code runs without errors
- Save your notebook before submitting
:link: Submit your exercise here
:information_source: Info Your submission helps us track your progress and provide feedback. Make sure to submit even if you couldn't complete all steps - partial credit is given!