A way to iterate over rows of a pandas data frame in Python is by using the function iterrows(), items() or itertuples():
Table of contents
Let's consider the following pandas data frame:
>>> import pandas as pd>>> data = {'Country':['USA', 'Spain', 'France', 'Canada'], 'Age':[10, 39, 21, 70]}>>> df = pd.DataFrame(data, index =['Bob', 'Jack', 'Ben', 'Paul'])>>> dfAge CountryBob 10 USAJack 39 SpainBen 21 FrancePaul 70 Canada
Using iterrows()
Pandas' DataFrame.iterrows() is a useful method for looping through each row of a Dataframe. It returns an iterator yielding each index value along with a series containing the data in each row. This allows you to easily access and modify the values in any given row.
>>> for index, row in df.iterrows():... print(index)... print(row)...BobAge 10Country USAName: Bob, dtype: objectJackAge 39Country SpainName: Jack, dtype: objectBenAge 21Country FranceName: Ben, dtype: objectPaulAge 70Country CanadaName: Paul, dtype: object
Using items()
To get the variation of columns in function of the data frame indexes, a solution is to use items():
>>> for label, content in df.items():... print(label)... print(content)...AgeBob 10Jack 39Ben 21Paul 70Name: Age, dtype: int64CountryBob USAJack SpainBen FrancePaul CanadaName: Country, dtype: object
To focus on a given column:
>>> for label, content in df['Age'].items():... print(label,content)...Bob 10Jack 39Ben 21Paul 70
Using itertuples()
Another solution is to use itertuples():
>>> for row in df.itertuples():... print(row)...Pandas(Index='Bob', Age=10, Country='USA')Pandas(Index='Jack', Age=39, Country='Spain')Pandas(Index='Ben', Age=21, Country='France')Pandas(Index='Paul', Age=70, Country='Canada')
Example 2:
>>> for row in df.itertuples():... print(row[0],row[2])...Bob USAJack SpainBen FrancePaul Canada
Example 3:
>>> for row in df.itertuples():... print(row.Age)...10392170
References
| Links | Site |
|---|---|
| pandas.DataFrame.iterrows | pandas doc |
| pandas.DataFrame.items | pandas doc |
| pandas.DataFrame.itertuples | pandas doc |
