Session Ready
Exercise

Iterating with .itertuples()

Remember, .itertuples() returns each DataFrame row as a special data type called a namedtuple. You can look up an attribute within a namedtuple with a special syntax. Let's practice working with namedtuples.

A pandas DataFrame has been loaded into your session called rangers_df. This DataFrame contains the stats ('Team', 'League', 'Year', 'RS', 'RA', 'W', 'G', and 'Playoffs') for the Major League baseball team named the Texas Rangers (abbreviated as 'TEX').

Instructions 1/3
undefined XP
  • 1

    Use .itertuples() to loop over rangers_df and print each row.

    • 2

      Loop over rangers_df with .itertuples() and save each row's Index, Year, and Wins (W) attribute as i, year, and wins.

    • 3

      Now, loop over rangers_df and print these values only for those rows where the Rangers made the playoffs.