Get startedGet started for free

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!

This exercise is part of the course

Writing Efficient Code with pandas

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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(___ - ___))
Edit and Run Code