8.1. Exercises

Practicing Slicing and Selecting Using Index Position

Here is our fruit_salad data again:

           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

Practicing Slicing Using Index Position

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.

Use the output of the following code chunk to help complete the next tasks.

In previous questions, we tried slicing using index labels on our Canack dataset. Let’s try something similar but using the index positions.

Tasks:

  • Slice the players Jacob Markstrom to Tim Schaller and the columns Player to Height.
  • Save the new sliced dataframe as object skilled_players.
  • Display it.
Hint 1
  • Are you using .iloc[]?
  • Are you using the correct dataframe positions? Are you counting starting from 0?
  • Are you going 1 index past the bound you want?
Fully worked solution:


These unfortunate Canuck players have various forms of injuries, so let’s make a dataframe of players who have injuries by selecting them using their index position.

Tasks:

  • Select Antoine Roussel, Thatcher Demko, Jake Virtanen and Jay Beagle with only columns Player, Birth Date, Experience and Salary in the specified order.
  • Save this dataframe as object injured_players.
  • Don’t forget to display it.
Hint 1
  • Are you using .iloc[]?
  • Are you using the correct dataframe positions? Are you counting starting from 0?
  • Are you using two sets of [] square brackets within your iloc brackets?
Fully worked solution: