26 Oct Iterate over rows and columns in a Pandas DataFrame
In this lesson, we will learn how to iterate over rows and columns in a Pandas DataFrame. Let’s see examples of how to:
- Iterate over rows
- Iterate over columns
Iterate over rows
To iterate over the rows, use the following methods in Pandas:
- iterrows(): To Iterate over the rows
- itertuples(): To Iterate over the rows
Pandas iterrows() to iterate over rows
To iterate over rows, use the Python Pandas iterrows() method. Here, we are only displaying the student names from the student records. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import pandas as pd # Dataset data = { 'id': ["S01", "S02", "S03", "S04", "S05"], 'student': ["Amit", "John", "Jacob", "David", "Steve"], 'rank': [1, 4, 3, 5, 2] } dataFrame = pd.DataFrame(data) print("Student Records\n", dataFrame) # Iterate over rows in the DataFrames print("\nDisplay the rows") for row in dataFrame.iterrows(): print(row) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Student Records id student rank 0 S01 Amit 1 1 S02 John 4 2 S03 Jacob 3 3 S04 David 5 4 S05 Steve 2 Display the rows (0, id S01 student Amit rank 1 Name: 0, dtype: object) (1, id S02 student John rank 4 Name: 1, dtype: object) (2, id S03 student Jacob rank 3 Name: 2, dtype: object) (3, id S04 student David rank 5 Name: 3, dtype: object) (4, id S05 student Steve rank 2 Name: 4, dtype: object) |
Pandas itertuples() to iterate over rows
To iterate over rows, use the Python Pandas itertuples() method. Here, each row is returned as a Python Tuple object. Here, we are only displaying the student names from the student records
Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import pandas as pd # Dataset data = { 'id': ["S01", "S02", "S03", "S04", "S05"], 'student': ["Amit", "John", "Jacob", "David", "Steve"], 'rank': [1, 4, 3, 5, 2] } dataFrame = pd.DataFrame(data) print("Student Records\n", dataFrame) # Iterate over rows in the DataFrames print("\nDisplay records as a Tuple object") for row in dataFrame.itertuples(): print(row) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Student Records id student rank 0 S01 Amit 1 1 S02 John 4 2 S03 Jacob 3 3 S04 David 5 4 S05 Steve 2 Display records as a Python object Pandas(Index=0, id='S01', student='Amit', rank=1) Pandas(Index=1, id='S02', student='John', rank=4) Pandas(Index=2, id='S03', student='Jacob', rank=3) Pandas(Index=3, id='S04', student='David', rank=5) Pandas(Index=4, id='S05', student='Steve', rank=2) |
Iterate over columns
To iterate over columns, use the following methods in Pandas:
- items(): To Iterate over the columns
- iteritems(): To Iterate over the columns (Deprecated now)
Pandas items() to iterate over columns
To iterate each and every column, use the Pandas items() method. The result will display a label object that is the name of the column and a column object that is what you have in the column.
Let us see an example to iterate over columns:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import pandas as pd # Dataset data = { 'id': ["S01", "S02", "S03", "S04", "S05"], 'student': ["Amit", "John", "Jacob", "David", "Steve"], 'rank': [1, 4, 3, 5, 2] } dataFrame = pd.DataFrame(data) print("Student Records\n") # Iterate over columns in the DataFrame for a, b in dataFrame.items(): print(a) print(b) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Student Records id 0 S01 1 S02 2 S03 3 S04 4 S05 Name: id, dtype: object student 0 Amit 1 John 2 Jacob 3 David 4 Steve Name: student, dtype: object rank 0 1 1 4 2 3 3 5 4 2 Name: rank, dtype: int64 |
Pandas iteritems() to iterate over columns
The Pandas itertems() method was deprecated. Use the items() as shown above to iterate over columns.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments