3.1. Exercises

Slicing and Dicing Practice

My dataframe object name is fruit_salad.

           name    colour    location    seed   shape  sweetness   water-content  weight
0         apple       red     canada    True   round     True          84         100
1        banana    yellow     mexico   False    long     True          75         120
2    cantaloupe    orange      spain    True   round     True          90        1360
3  dragon-fruit   magenta      china    True   round    False          96         600
4    elderberry    purple    austria   False   round     True          80           5
5           fig    purple     turkey   False    oval    False          78          40
6         guava     green     mexico    True    oval     True          83         450
7   huckleberry      blue     canada    True   round     True          73           5
8          kiwi     brown      china    True   round     True          80          76
9         lemon    yellow     mexico   False    oval    False          83          65

Coding Questions

Instructions:
Running a coding exercise for the first time, could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

This following code chunk will be used to give you an output of the data hockey_players. This will help answer and code in the exercises following it.

Practicing Rows Slicing

Using our hockey_players data from the last few questions, let’s try slicing it.

Tasks:

  • Select the players Guillaume Brisebois to Quinn Hughes.
  • Save the new sliced dataframe as object benched_players.
  • Display it.
Hint 1
  • Are you using .loc[]?
  • Are you using the correct dataframe labels? Check your spelling and punctuation in row and column labels.
Fully worked solution:

Practicing Column Slicing

Using our hockey_players data from the last few questions, let’s try slicing it.

Tasks:

  • Select the players Adam Gaudette to Brandon Sutter and the columns Player, No., Age, Height, Weight and Country.
  • Save the new sliced dataframe as object star_players.
  • Display it.
Hint 1
  • Are you using .loc[]?
  • Are you using the correct dataframe labels? Check your spelling and punctuation in row and column labels.
  • Did you slice both columns and rows?
  • Are you using “quotations”?
Fully worked solution: