modin.pandas.DataFrame.itertuples¶
- DataFrame.itertuples(index: bool = True, name: str | None = 'Pandas') Iterable[tuple[Any, ...]] [source] (https://github.com/snowflakedb/snowpark-python/blob/v1.26.0/snowpark-python/src/snowflake/snowpark/modin/plugin/extensions/dataframe_overrides.py#L2187-L2222)¶
Iterate over DataFrame rows as namedtuples.
- Parameters:
index (bool, default True) – If True, return the index as the first element of the tuple.
name (str or None, default "Pandas") – The name of the returned namedtuples or None to return regular tuples.
- Returns:
An object to iterate over namedtuples for each row in the DataFrame with the first field possibly being the index and following fields being the column values.
- Return type:
iterator
See also
DataFrame.iterrows
Iterate over DataFrame rows as (index, Series) pairs.
DataFrame.items
Iterate over (column name, Series) pairs.
Notes
Iterating over rows is an antipattern in Snowpark pandas and pandas. Use df.apply() or other aggregation methods when possible instead of iterating over a DataFrame. Iterators and for loops do not scale well.
The column names will be renamed to positional names if they are invalid Python identifiers, repeated, or start with an underscore (follows namedtuple rules).
Examples
>>> df = pd.DataFrame({'num_legs': [4, 2], 'num_wings': [0, 2]}, index=['dog', 'hawk']) >>> df num_legs num_wings dog 4 0 hawk 2 2 >>> for row in df.itertuples(): ... print(row) ... Pandas(Index='dog', num_legs=4, num_wings=0) Pandas(Index='hawk', num_legs=2, num_wings=2)
By setting the index parameter to False we can remove the index as the first element of the tuple: >>> for row in df.itertuples(index=False): … print(row) … Pandas(num_legs=4, num_wings=0) Pandas(num_legs=2, num_wings=2)
>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=['cobra', 'viper', 'sidewinder'], ... columns=['max_speed', 'shield']) >>> df max_speed shield cobra 1 2 viper 4 5 sidewinder 7 8
>>> for row in df.itertuples(): ... print(row) ... Pandas(Index='cobra', max_speed=1, shield=2) Pandas(Index='viper', max_speed=4, shield=5) Pandas(Index='sidewinder', max_speed=7, shield=8)
Rename the namedtuple and create it without the index values. >>> for row in df.itertuples(name=”NewName”, index=False): … print(row) … NewName(max_speed=1, shield=2) NewName(max_speed=4, shield=5) NewName(max_speed=7, shield=8)
When name is None, return a regular tuple. >>> for row in df.itertuples(name=None): … print(row) … (‘cobra’, 1, 2) (‘viper’, 4, 5) (‘sidewinder’, 7, 8)