Updated April 4, 2023
Introduction to Pandas iterrows()
A dataframe is a data structure formulated by means of the row, column format. there may be a need at some instances to loop through each row associated in the dataframe. this can be achieved by means of the iterrows() function in the pandas library. the iterrows() function when used referring its corresponding dataframe it allows to travel through and access all the rows associated with that dataframe. The outcome of the iterrows() method is of iter format so a loop or the next function can be used to view the outcomes of this function.
Syntax:
DataFrame.iterrows(self)
Output returned by iterrows():
Parameter | Description |
Index | Mentions the row index |
data | Defines the data of the row |
Examples of Pandas iterrows()
Following are the example are given below:
Example #1
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'Emp_No' : ['Emp1','Emp2','Emp3','Emp4'],
'Employee_Name' : ['Arun', 'selva', 'rakesh', 'arjith'],
'Employee_dept' : ['CAD', 'CAD', 'DEV', 'CAD']})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
for row in Core_Dataframe.iterrows():
print("")
print(row)
Output:
Code Explanation: In this example, the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console. We can notice at this instance the dataframe holds details like employee number, employee name, and employee department. The iterrows() function offers the flexibility to sophisticatedly iterate through these rows of the dataframe. So every row of the iterrows() functions are iterated through a compact for loop and printed on to the console.
Example #2
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'A' : [ 1, 6, 11, 15, 21, 26],
'B' : [2, 7, 12, 17, 22, 27],
'C' : [3, 8, 13, 18, 23, 28],
'D' : [4, 9, 14, 19, 24, 29],
'E' : [5, 10, 15, 20, 25, 30]})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
for row in Core_Dataframe.iterrows():
print("")
print(row)
Output:
Code Explanation: In this example, the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console. We can notice at this instance the dataframe holds a random set of numbers and alphabetic values of columns associated with it. The iterrows() function offers the flexibility to sophisticatedly iterate through these rows of the dataframe. So every row of the iterrows() functions are iterated through a compact for loop and printed on to the console.
Example #3
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame( {
'name': ['Alan Xavier', 'Annabella', 'Janawong', 'Yistien', 'Robin sheperd', 'Amala paul', 'Nori'],
'city': ['california', 'Toronto', 'ontario', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [51, 38, 23, 64, 18, 31, 47],
'py-score': [82.0, 73.0, 81.0, 30.0, 48.0, 61.0, 84.0] })
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
for row in Core_Dataframe.iterrows():
print("")
print(row[1])
Output:
Code Explanation: In this example, the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console. We can notice at this instance the dataframe holds random people information and the py_score value of those people. the key columns used in this dataframe are name, age, city, and py-score value. The iterrows() function offers the flexibility to sophisticatedly iterate through these rows of the dataframe. So every row of the iterrows() functions are iterated through a compact for loop and printed on to the console. Since the data used here is not specific to any single data type the data are tagged under the data type value object.
Example #4
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'Column1' : [ 'A', 'B', 'C', 'D', 'E', 'F'],
'Column2' : [ 'G', 'H', 'I', 'J', 'K', 'L'],
'Column3' : [ 'M', 'N', 'O', 'P', 'Q', 'R'],
'Column4' : [ 'S', 'T', 'U', 'V', 'W', 'X'],
'Column5' : [ 'Y', 'Z', None, None, None, None]})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
for row in Core_Dataframe.iterrows():
print("")
print(row)
Output:
Code Explanation: In this example, the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console. We can notice in this example the dataframe is associated with the values of alphabets in the English dictionary. Every column in the dictionary is tagged with suitable column names. The iterrows() function offers the flexibility to sophisticatedly iterate through these rows of the dataframe. So every row of the iterrows() functions are iterated through a compact for loop and printed on to the console. Since the data used here is not specific to any single data type the data are tagged under the data type value object.
Example #5
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'A' : [ 1.23, 6.66, 11.55, 15.44, 21.44, 26.4 ],
'B' : [ 2.345, 745.5, 12.4, 17.34, 22.35, 27.44 ],
'C' : [ 3.67, 8, 13.4, 18, 23, 28.44 ],
'D' : [ 4.6788, 923.3, 14.5, 19, 24, 29.44 ],
'E' : [ 5.3, 10.344, 15.556, 20.6775, 25.4455, 30.3 ]})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print("")
for row in Core_Dataframe.iterrows():
print("")
print(row)
Output:
Code Explanation: In this example, the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console. A typical float dataset is used in this instance. The iterrows() function offers the flexibility to sophisticatedly iterate through these rows of the dataframe. So every row of the iterrows() functions are iterated through a compact for loop and printed on to the console. We can notice form the printed output from the console that all the rows from the core dataframe are tagged with the data type float.
Recommended Articles
We hope that this EDUCBA information on “Pandas iterrows()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.