Pandas iterate over rows - Python - Code Example & Live Demo

Pandas iterate over rows - Python - Code Example & Live Demo

Pandas iterate over rows - Python - Code Example & Live Demo. Check out code example and live demo to see it in action.

Python pandas provides iterrows() function which can be used to iterate over rows in dataframe. Consider this example –

import pandas as pd
df = pd.DataFrame({'marvel': ['Thor', 'Hulk'], 'dc': ['Flash', 'Cyborg']})
print(df)

PythonCopy

In order to iterate row by row, we need to run a loop using iterrows() function. For now, our dataframe has 2 lists – marvel and dc. When we run iterrows(), it will fetch Thor & Flash in first iteration and Hulk & Cyborg in second iteration.

Here is the code –

for index, row in df.iterrows():
    print(row['marvel'], row['dc'])

PythonCopy

python python dataframe python pandas python-short

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python Pandas Objects - Pandas Series and Pandas Dataframe

In this post, we will learn about pandas’ data structures/objects. Pandas provide two type of data structures:- ### Pandas Series Pandas Series is a one dimensional indexed data, which can hold datatypes like integer, string, boolean, float...

top 30 Python Tips and Tricks for Beginners

In this post, we'll learn top 30 Python Tips and Tricks for Beginners

Pandas in Python

Pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.

Lambda, Map, Filter functions in python

You can learn how to use Lambda,Map,Filter function in python with Advance code examples. Please read this article

How to Rename Columns in Pandas Python?

To rename column in pandas either use df.columns=[new columns] or you can use rename function like df.rename=(column={'old': 'new'}).