In Python, the DataFrame.__iter__()
method of Pandas is used to create an iterator instance that iterates through DataFrame or series info axis.
DataFrame.__iter__()
This method does not take any argument value.
This method returns a Python iterator object.
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.
# Programimport pandas as pd# initializing a nested listdata = [['China', 15, 2021], ['America', 21, 2021], ['Pakistan', 0.4, 2020]]# Creating the DataFramedf = pd.DataFrame(data, columns = ['Country', 'GDP', 'Year'])# invoking __iter__()for x in df.__iter__():print(x)
pd.DataFrame()
to create a DataFrame on the above created nested list and column names.