Get startedGet started for free

pandas vectorization in action

In this exercise, you will apply vectorization over pandas series to:

  • calculate the mean rank of all the cards in each hand (row)
  • calculate the mean rank of each of the 5 cards in each hand (column)

You will use the poker_hands dataset once again to compare both methods' efficiency.

This exercise is part of the course

Writing Efficient Code with pandas

View Course

Exercise instructions

  • Calculate the mean rank in each hand.
  • Calculate the mean rank of each of the 5 card in all hands.

Hands-on interactive exercise

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

# Calculate the mean rank in each hand
row_start_time = time.time()
mean_r = poker_hands[['R1', 'R2', 'R3', 'R4', 'R5']].____(axis=____)
print("Time using pandas vectorization for rows: {} sec".format(time.time() - row_start_time))
print(mean_r.head())

# Calculate the mean rank of each of the 5 card in all hands
col_start_time = time.time()
mean_c = poker_hands[['R1', 'R2', 'R3', ____, ____]].____(____=____)
print("Time using pandas vectorization for columns: {} sec".format(time.time() - col_start_time))
print(mean_c.head())
Edit and Run Code