
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate Over Rows and Columns in Pandas DataFrame
In this article we will learn about pandas, DataFrame and how we can iterate over rows and columns in the pandas DataFrame using various methods. Iteration is a basic operation we do when we have tabular data containing rows and columns.
To install the pandas in the system execute the command in cmd.
pip install pandas
Method 1. Using Iterrows() Method.
Example
import pandas as pd df = pd.DataFrame({'Name': ['Kalyan', 'Gungun', 'Sona', 'Ram'], 'Age': [21, 20, 23, 23], 'Roll': [12, 13, 14, 15], 'Game': ['Cricket', 'Lodu', 'Chess', 'Carrom']}) print("No", "Name", "Age", "Roll", "Game") for index, row in df.iterrows(): print(index, row['Name'], row['Age'], row['Roll'], row['Game'])
Output
No Name Age Roll Game 0 Kalyan 21 12 Cricket 1 Gungun 20 13 Lodu 2 Sona 23 14 Chess 3 Ram 23 15 Carrom
Explanation
In the above program we create a dataframe which contains columns as Name, Age,Roll and Game. We use the iterrows() method which allows us to iterate over the dataframe and returns rows as a tuple containing index and row data. Finally we print the data which we get after iteration.
Method 2. Using itertuples() method.
Example
import pandas as pd df = pd.DataFrame({'Name': ['Kalyan', 'Gungun', 'Sona', 'Ram'], 'Age': [21, 20, 23, 23], 'Roll': [12, 13, 14, 15], 'Game': ['Cricket', 'Lodu', 'Chess', 'Carrom']}) print("No", "Name", "Age", "Roll", "Game") for row in df.itertuples(): print(row.Index, row.Name, row.Age, row.Roll, row.Game)
Output
No Name Age Roll Game 0 Kalyan 21 12 Cricket 1 Gungun 20 13 Lodu 2 Sona 23 14 Chess 3 Ram 23 15 Carrom
Explanation
In the above program we create a dataframe which contains columns as Name, Age,Roll and Game. We use the itertuples() method which allows us to iterate over rows and returns named tuples for each row and column values. Finally we print the data which we get after iteration.
Method 3. Using Function Apply() With Axis=1 on row Iteration.
Example
import pandas as pd df = pd.DataFrame({'Name': ['Kalyan', 'Gungun', 'Sona', 'Ram'], 'Age': [21, 20, 23, 23], 'Roll': [12, 13, 14, 15], 'Game': ['Cricket', 'Lodu', 'Chess', 'Carrom']}) print("Name", "Age", "Roll", "Game") def process_row(row): print(row['Name'], row['Age'], row['Roll'], row['Game']) df.apply(process_row, axis=1)
Output
Name Age Roll Game Kalyan 21 12 Cricket Gungun 20 13 Lodu Sona 23 14 Chess Ram 23 15 Carrom
Explanation
In the above program we create a dataframe which contains columns as Name, Age,Roll and Game. we use apply() function which allows us to apply custom function on each row of the dataframe. When we write axis=1 then we can iterate over the row and perform row wise operation.
Method 4. Using Function Apply() with Axis=0 on Column Iteration.
Example
import pandas as pd df = pd.DataFrame({'Name': ['Kalyan', 'Gungun', 'Sona', 'Ram'], 'Age': [21, 20, 23, 23], 'Roll': [12, 13, 14, 15], 'Game': ['Cricket', 'Lodu', 'Chess', 'Carrom']}) def process_column(column): print(column) df.apply(process_column, axis=0)
Output
0 Kalyan 1 Gungun 2 Sona 3 Ram Name: Name, dtype: object 0 21 1 20 2 23 3 23 Name: Age, dtype: int64 0 12 1 13 2 14 3 15 Name: Roll, dtype: int64 0 Cricket 1 Lodu 2 Chess 3 Carrom Name: Game, dtype: object
Explanation
In the above program we created dataframe which contains columns as Name, Age, Roll and Game. We use apply() function which allows us to apply custom function on each column of the dataframe. When we write axis=0 then we can iterate over the row and perform row wise operation. It is similar to the row wise iteration (written in previous example).
Method 5. Using Lambda Function with Apply() Function.
Example
import pandas as pd df = pd.DataFrame({'Name': ['Kalyan', 'Gungun', 'Sona', 'Ram'], 'Age': [21, 20, 23, 23], 'Roll': [12, 13, 14, 15], 'Game': ['Cricket', 'Lodu', 'Chess', 'Carrom']}) print("Name", "Age", "Roll", "Game") df.apply(lambda row: print(row['Name'], row['Age'], row['Roll'], row['Game']), axis=1)
Output
Name Age Roll Game Kalyan 21 12 Cricket Gungun 20 13 Lodu Sona 23 14 Chess Ram 23 15 Carrom
Explanation
In the above program we create a dataframe which contains columns as Name, Age,Roll and Game. We use lambda function along with the apply() method for efficient iteration over the rows in the dataframe.
So, we get to know about various methods using which we can iterate over rows and columns in Pandas DataFrame. Though in the last method nditer() is not always suitable compared to other methods like iterrows(), itertuples(), or apply(). nditer() method is useful for large size arrays and when you need more control over iteration.