To iterate over rows of a pandas data frame in python, a solution is to use 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'])
>>> df
Age Country
Bob 10 USA
Jack 39 Spain
Ben 21 France
Paul 70 Canada
Using iterrows()
To go through all rows of the above data frame and print all associated columns, a solution is to use iterrows():
>>> for index, row in df.iterrows():
... print(index)
... print(row)
...
Bob
Age 10
Country USA
Name: Bob, dtype: object
Jack
Age 39
Country Spain
Name: Jack, dtype: object
Ben
Age 21
Country France
Name: Ben, dtype: object
Paul
Age 70
Country Canada
Name: 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)
...
Age
Bob 10
Jack 39
Ben 21
Paul 70
Name: Age, dtype: int64
Country
Bob USA
Jack Spain
Ben France
Paul Canada
Name: Country, dtype: object
To focus on a given column:
>>> for label, content in df['Age'].items():
... print(label,content)
...
Bob 10
Jack 39
Ben 21
Paul 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 USA
Jack Spain
Ben France
Paul Canada
Example 3:
>>> for row in df.itertuples():
... print(row.Age)
...
10
39
21
70
References
Links | Site |
---|---|
pandas.DataFrame.iterrows | pandas doc |
pandas.DataFrame.items | pandas doc |
pandas.DataFrame.itertuples | pandas doc |
How to iterate over rows in a DataFrame in Pandas? | stackoverflow |