Aan de slagGa gratis aan de slag

Column selection: .iloc[] vs by name

In the previous exercise, you saw how the .loc[] and .iloc[] functions can be used to locate specific rows of a DataFrame (based on the index). Turns out, the .iloc[] function performs a lot faster (~ 2 times) for this task!

Another important task is to find the faster function to select the targeted features (columns) of a DataFrame. In this exercise, we will compare the following:

  • using the index locator .iloc()
  • using the names of the columns While we can use both functions to perform the same task, we are interested in which is the most efficient in terms of speed.

In this exercise, you will continue working with the poker data which is stored in poker_hands. Take a second to examine the structure of this DataFrame by calling poker_hands.head() in the console!

Deze oefening maakt deel uit van de cursus

Writing Efficient Code with pandas

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Use .iloc to select the first, fourth, fifth, seventh and eighth column and record the times before and after
iloc_start_time = ___
cols = poker_hands.___[___,[0,3,___,___,___]]
iloc_end_time = ___

# Print the time it took
print("Time using .iloc[] : {} sec".format(___ - ___))
Code bewerken en uitvoeren