- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.iterrows#
- DataFrame.iterrows()[source]#
Iterate over DataFrame rows as (index, Series) pairs.
- Yields:
- indexlabel or tuple of label
The index of the row. A tuple for aMultiIndex.
- dataSeries
The data of the row as a Series.
See also
DataFrame.itertuples
Iterate over DataFrame rows as namedtuples of the values.
DataFrame.items
Iterate over (column name, Series) pairs.
Notes
Because
iterrows
returns a Series for each row,it doesnot preserve dtypes across the rows (dtypes arepreserved across columns for DataFrames).To preserve dtypes while iterating over the rows, it is betterto use
itertuples()
which returns namedtuples of the valuesand which is generally faster thaniterrows
.You shouldnever modify something you are iterating over.This is not guaranteed to work in all cases. Depending on thedata types, the iterator returns a copy and not a view, and writingto it will have no effect.
Examples
>>>df=pd.DataFrame([[1,1.5]],columns=['int','float'])>>>row=next(df.iterrows())[1]>>>rowint 1.0float 1.5Name: 0, dtype: float64>>>print(row['int'].dtype)float64>>>print(df['int'].dtype)int64