1. Learn
  2. /
  3. Courses
  4. /
  5. Writing Efficient Code with pandas

Connected

Exercise

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!

Instructions 1/3

undefined XP
    1
    2
    3
  • Use the .iloc indexer to select the first, fourth, fifth, seventh and eighth column ('S1', 'R2', 'S3', 'S4', 'R4') of the DataFrame poker_hands by their index and find the time it took.