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'
).
This exercise is part of the course
Writing Efficient Python Code
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Loop over the DataFrame and print each row
for ____ in ____.____():
print(____)