Aan de slagGa gratis aan de slag

The iterrows() function for looping

You just saw how to create a generator out of a pandas DataFrame. You will now use this generator and see how to take advantage of that method of looping through a pandas DataFrame, still using the poker_hands dataset.

Specifically, we want the sum of the ranks of all the cards, if the index of the hand is an odd number. The ranks of the cards are located in the odd columns of the DataFrame.

Deze oefening maakt deel uit van de cursus

Writing Efficient Code with pandas

Cursus bekijken

Oefeninstructies

  • Check if the hand index is an odd number.
  • If it is, calculate the sum of the rank of all the cards in that hand. It could take a little longer than usual to compute the results.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

data_generator = poker_hands.iterrows()

for index, values in data_generator:
  	# Check if index is odd
    if ____:
      	# Sum the ranks of all the cards
        hand_sum = sum([____[1], ____[3], ____, ____, ____])
Code bewerken en uitvoeren