Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dataframe
pandas
communitycreator

What is the DataFrame.__iter__() method of Pandas in Python?

AKASH BAJWA

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In Python, the DataFrame.__iter__() method of Pandas is used to create an iterator instance that iterates through DataFrame or series info axis.

Syntax


DataFrame.__iter__()
Syntax

Parameters

This method does not take any argument value.

Return value

This method returns a Python iterator object.

Example

In the code below, we use this method to iterate through a Pandas DataFrame. We have a DataFrame that includes country names and yearly GDP values.

# Program
import pandas as pd
# initializing a nested list
data = [['China', 15, 2021], ['America', 21, 2021], ['Pakistan', 0.4, 2020]]
# Creating the DataFrame
df = pd.DataFrame(data, columns = ['Country', 'GDP', 'Year'])
# invoking __iter__()
for x in df.__iter__():
print(x)
Implementation of DataFrame.__iter__()

Explanation

  • Line4: We create a nested list to create a DataFrame.
  • Line 6: We invoke pd.DataFrame() to create a DataFrame on the above created nested list and column names.
  • Line 8: We iterate through the DataFrame info axis.

RELATED TAGS

dataframe
pandas
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring