- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to iterate over rows in a DataFrame in Pandas?
To iterate rows in a DataFrame in Pandas, we can use the iterrows() method, which will iterate over DataFrame rows as (index, Series) pairs.
Steps
Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
Iterate df using df.iterrows() method.
Print each row with index.
Example
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 1, 9], "y": [4, 1, 5, 10], "z": [4, 1, 5, 0] } ) print "Given DataFrame:
", df for index, row in df.iterrows(): print "Row ", index, "contains: " print row["x"], row["y"], row["z"]
Output
Given DataFrame: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 Row 0 contains: 5 4 4 Row 1 contains: 2 1 1 Row 2 contains: 1 5 5 Row 3 contains: 9 10 0
- Related Articles
- How to access a group of rows in a Pandas DataFrame?
- Python Pandas - How to append rows to a DataFrame
- Python - How to group DataFrame rows into list in Pandas?
- How to append new rows to DataFrame using a Template In Python Pandas
- Python Pandas - How to select multiple rows from a DataFrame
- How to iterate over a Hashmap in Kotlin?
- How to iterate over a list in Java?
- How to plot certain rows of a Pandas dataframe using Matplotlib?
- Python - How to drop the null rows from a Pandas DataFrame
- Delete the first three rows of a DataFrame in Pandas
- Python Pandas – Count the rows and columns in a DataFrame
- Python Pandas - How to select rows from a DataFrame by integer location
- Python - Ranking Rows of Pandas DataFrame
- How to iterate over a Java list?
- How to iterate over a C# dictionary?

Advertisements