- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.itertuples#
- DataFrame.itertuples(index=True,name='Pandas')[source]#
Iterate over DataFrame rows as namedtuples.
- Parameters:
- indexbool, default True
If True, return the index as the first element of the tuple.
- namestr or None, default “Pandas”
The name of the returned namedtuples or None to return regulartuples.
- Returns:
- iterator
An object to iterate over namedtuples for each row in theDataFrame with the first field possibly being the index andfollowing fields being the column values.
See also
DataFrame.iterrowsIterate over DataFrame rows as (index, Series) pairs.
DataFrame.itemsIterate over (column name, Series) pairs.
Notes
The column names will be renamed to positional names if they areinvalid Python identifiers, repeated, or start with an underscore.
Examples
>>>df=pd.DataFrame({'num_legs':[4,2],'num_wings':[0,2]},...index=['dog','hawk'])>>>df num_legs num_wingsdog 4 0hawk 2 2>>>forrowindf.itertuples():...print(row)...Pandas(Index='dog', num_legs=4, num_wings=0)Pandas(Index='hawk', num_legs=2, num_wings=2)
By setting theindex parameter to False we can remove the indexas the first element of the tuple:
>>>forrowindf.itertuples(index=False):...print(row)...Pandas(num_legs=4, num_wings=0)Pandas(num_legs=2, num_wings=2)
With thename parameter set we set a custom name for the yieldednamedtuples:
>>>forrowindf.itertuples(name='Animal'):...print(row)...Animal(Index='dog', num_legs=4, num_wings=0)Animal(Index='hawk', num_legs=2, num_wings=2)