In this Python Pandas tutorial, we will learn how to use the iterrows() function in Pandas DataFrame using Python. Also, we will cover these topics.
- Pandas DataFrame iterrows index
- Pandas DataFrame iterrows slow
- Pandas DataFrame iterrows update value
- Pandas DataFrame iterrows add column
- Pandas DataFrame iterrows reverse
- Pandas series iterrows
- Pandas update DataFrame with iterrows
- Pandas iterrows return DataFrame
- Pandas DataFrame itertuples vs iterrows
- Pandas DataFrame iterrows set value
- Pandas DataFrame iterrows for loop
- Pandas DataFrame iterrows change value
Python DataFrame Iterrows
- In this Program, we will discuss how to iterate over rows of a DataFrame by using the iterrows() method.
- In Python, the Pandas DataFrame.iterrows() method is used to loop through each row of the Pandas DataFrame and it always returns an iterator that stores data of each row.
- There are various method to iterate over rows of a DataFrame.
- By using iterrows() method
- By using itertuple() method
By using iterrows() method
In Python, Pandas has an iterrows() method that will help the user to iterate a loop through each row and column of a Pandas DataFrame.
Syntax:
Here is the Syntax of iterrows() method
DataFrame.iterrows()
- Index: Index of the row in Pandas DataFrame and a tuple of the multiindex.
- Data: It always return the row data as a Pandas Series.
Example:
Let’s take an example and check how to iterate through rows with Pandas
import pandas as pd
df = pd.DataFrame([[26,16,18,8.7,2.9,1.9]], columns=[
'George', 'John', 'Micheal', 'Oliva', 'Chris', 'Hemosworth'])
new_itr = next(df.iterrows())[1]
print(new_itr)
In the above code first, we have imported a pandas module then create a dataframe along with a column name. Now iterate over the data frame rows by using the iterrows() method.
Here is the Screenshot of the following given code
Read, Python convert DataFrame to list
By using itertuple() method
- In Python, the itertuple() method iterates the rows and columns of the Pandas DataFrame as namedtuples. When we are using this function in Pandas DataFrame, it returns a map object.
- In this method, the first value of the tuple will be the row index value, and the remaining values are left as row values.
Syntax:
Here is the Syntax of itertuple() method
DataFrame.itertuples(
index=True,
name='Pandas'
)
- It Consists of a few parameters
- Index: If the value is True then it will return the index as the first value of the tuple.
- Name: This parameter specifies the returned tuples.
Source Code:
import pandas as pd
new_dt = {
"Country_name": ["Australia", "Newzealand", "Germany"],
"new_values": [672, 193, 881]
}
result = pd.DataFrame(new_dt)
for new_row in result.itertuples():
print(new_row)
In the above Program First, we will create a dictionary that contains elements in the form of key and value. Now create a DataFrame and assign a dictionary ‘new_dt’. Once you will print ‘new_row’ then the output will display in the form tuple.
Here is the Execution of the following given code
Read: Pandas Delete Column
Pandas DataFrame iterrows index
- Let us see how to iterate over rows and columns of a DataFrame with an index.
- By using the iterrows() function we can perform this particular task and in this example, we will create a DataFrame with five rows and iterate through using the iterate() method.
Source Code:
import pandas as pd
new_dt = pd.DataFrame([['Rose', 'Tulip', 'Lilly', 'Jasmine',
'Lotus']])
new_iteration = next(new_dt.iterrows())[1]
print(new_iteration)
Here is the Output of the following given code
By using for loop + iterrows() method
Here we can see how to iterate rows and columns of dataframe and also able to access the index of row by using the iterrows() method.
Source Code:
import pandas as pd
new_val = pd.DataFrame({
'Country_name': ['China', 'Japan', 'Malayasia', 'Bangladesh'],
'dict_val': [721, 618, 178, 389]})
for index, row in new_val.iterrows():
print(index, ': ', row['Country_name'], row['dict_val'])
You can refer to the below Screenshot
Read: How to Convert Pandas DataFrame to a Dictionary
Pandas DataFrame iterrows slow
- In this program, we will discuss why iterrows() method is slow.
- In Python iterrows performance is very slow as compared to the itertuples() method because when are applying multiple functions while iterating in iterrows() then each row has its own properties which make it’s slower.
- There is a various method in Python pandas that perform better as compare to iterrows method like (itertuple).
Pandas DataFrame iterrows update value
- Here we can see how to create a Pandas DataFrame and update while iterating row by row.
- In this example we have updated the contents of the dataframe and also need to iterate over the rows and columns of the Pandas DataFrame.
Source Code:
import pandas as pd
new_data = [(62, 19, 634, 189) ,
(156, 178, 156, 762) ,
(109, 447, 390, 643)
]
df = pd.DataFrame(new_data, columns=['George', 'Micheal' , 'Oliva', 'Elijah'])
for new_ind, select_row in df.iterrows():
df.at[new_ind , 'Oliva'] = select_row['Oliva'] * 3
print(df)
In the above program, we have updated each value in Column ‘Oliva’ by multiplying it with 3. Once you will print ‘df’ then the output will display in the form of an updated DataFrame.
Here is the implementation of the following given code
Read: Python Pandas Drop Rows
Pandas DataFrame iterrows add column
- Let us see how to add a column in a Pandas DataFrame by using iterrows() and the iloc method.
- By using the index position and iloc method we can solve this task and in this example, we have created a DataFrame and using row to add a new column in it.
- In Python, the iloc method is used to choose a specified row of datasets that helps to selects a value that belongs to a particular column from a dataframe.
Source Code:
import pandas as pd
import numpy as np
new_dt = pd.DataFrame(columns=list('mn'), data = np.random.randn(6,2))
print(new_dt)
for new_ind,row in new_dt.iterrows():
new_dt.loc[new_ind,'o'] = np.random.randint(0, 8)
print("Updated dataframe:")
print(new_dt)
In the above program, we have created an array by using np. random() function. Now I have to add a column in the existing array to do this we have to use the iloc method and assign a new column in it.
Here is the implementation of the following given Code
Read: Groupby in Python Pandas
Pandas DataFrame iterrows reverse
- Here we can see how to reverse a dataframe row by using the iloc method.
- By using iloc we can access each row by index position. In this example, we can use a loop through rows of dataframe by index in reverse.
Source Code:
import pandas as pd
Student_info = [('William', 689, 'Micheal', 591) ,
('John', 148, 'Potter' , 109) ,
('Hmsworth', 776, 'Chris', 314)
]
new_result = pd.DataFrame(Student_info, columns=['Stu_name', 'Stu_id', 'Stu2_name', 'Stu2_id'], index=['m', 'o', 'p'])
for m in range(new_result.shape[0] - 1, -1, -1):
Final_output = new_result.iloc[m]
print(Final_output.values)
In the above program first, we have imported a pandas module then create a list in which multiple tuples have been stored. Now we have to iterate over rows in the dataframe in reversing by applying for iloc and index position.
To do this task we have created a DataFrame ‘new_result’ and then loop through the last index to the 0th index. Once you will print ‘Final_output’ then the output will display to reverse the number of rows in a DataFrame.
You can refer to the below Screenshot
Read: Crosstab in Python Pandas
Pandas series iterrows
- Let us see how to iterate over rows in a Pandas DataFrame by using series.iterrows() method.
- In Python the series. iterrows method returns an iterable list or tuple (index, value).In Python, the iloc method is used to select a specified cell of the dataset or DataFrame.
Source Code:
import pandas as pd
Student_info = [('Oliva', 178, 'Canada', 223) ,
('Elijah', 567, 'Newyork' , 350) ,
('George', 921, 'Malayasia', 1334)
]
new_val = pd.DataFrame(Student_info, columns=['Stu_name', 'Stu_id', 'Stu_add', 'stu_age'])
for index in new_val.index:
val_series = new_val.loc[index]
print(val_series.values)
In the above example, we have to use the combination of for loop and iloc method to iterate over the dataframe column and rows.
Here is the Screenshot of the following given code
Another example of how to use series.iterrows() function in DataFrame.
This is another approach to use series.iterrows() method to iterate over rows in a DataFrame. It will always return an iterable tuple that contains the pairs of a series.
Source Code:
import pandas as pd
new_data = pd.Series(['m', 'n', 'o'])
for index, value in new_data.items():
print(f"Index_val : {index}, New_Value : {value}")
Here is the output of the following given Code
Read: Missing Data in Pandas in Python
Pandas update DataFrame with iterrows
- Here we can see how to update a Pandas DataFrame with iterrows() method.
- In Python, the iterrows() method will help the user to update the values or columns as per the given condition and in this example, we have used for loop to get each row of the Pandas DataFrame and the iterrows method always return an iterator that stores data of each row.
Example:
Let’s take an example and check how to update a DataFrame with iterrows
import pandas as pd
new_val = [(14, 21, 189, 49) ,
(981, 445,156,109) ,
(267, 871, 156,456)
]
df = pd.DataFrame(new_val, columns=['Chris', 'Hemsworth' , 'George', 'Elijah'])
for new_ind, select_row in df.iterrows():
df.at[new_ind , 'George'] = select_row['George'] * 4
print(df)
In the above example, we have to update each value in Column ‘George’ by multiplying it with 4. Once you will print ‘df’ then the output will display in the form of an updated DataFrame.
You can refer to the below Screenshot
Read: Python Pandas CSV Tutorial
Pandas iterrows return DataFrame
- In this Program, we will discuss how to return a Pandas DataFrame by using iterrrows() function in Python.
- In Python sometimes we need to iterate over the Pandas Dataframe columns and rows without using for loop method. So In this case we will use the iterrows() method to iterate over a row which is in the form of (index, series) pair.
Syntax:
Here is the syntax of the iterrows() method.
DataFrame.iterrows()
Source Code:
Let’s take an example and understand how to return a Series for each row
import pandas as pd
df = pd.DataFrame([['Banana', 'Cherry', 'Grapes', 'Oranges',
'Apple', 'litchi']])
new_val = next(df.iterrows())[1]
print(new_val)
In the above program, we iterate over the Pandas DataFrame and having no column name by using the iterrows() method.
Here is the screenshot of the following given code.
Read: Get First Key in dictionary Python
Pandas DataFrame itertuple vs iterrows
- Here we can see the difference between the itertuple and itertool method in Pandas DataFrame
- In Python, the itertuple() method iterates the rows of the Pandas DataFrame as named tuple. Whereas the iterrows() is a method that iterates over the rows and columns of Pandas DataFrame.
- In Python, the iterrows() method always returns a series while itertuple returns a named tuple. If you want to access the values using indexes and the getattr() method then the allocates values of namedtuple are ordered.
- In Python, both methods are a Pandas inbuilt function that iterates through over Pandas DataFrame. while iterating we can declare multiple functions by using the iterrows() method. Whereas itertuples() calls less number of function than iterrows().
Source Code:
import pandas as pd
new_dictionary = {'Student_name': ['Micheal', 'John', 'william', 'jonas'],
'Student_id': [66,24,91,118],
'Student_address': ['China', 'Japan', 'France', 'Ireland'],
'Student_age': [18,21,32,28]}
df = pd.DataFrame(new_dictionary, columns = ['Student_name','Student_id','Student_address','Student_age'])
for index, row in df.iterrows():
print (row['Student_name'], row['Student_age'])
print (row['Student_address'], row['Student_age'])
#itertuple
Employee_dictionary= {'Emp_name': ['Noah', 'Lijah', 'Hayden', 'Adam'],
'Emp_unique_id': [612, 883, 945, 7119],
'Desgination': ['Tester', 'Gamer', 'Quality assurance', 'Production'],
'Emp_add': ['Germany','Paris','Bangladesh','Moscow']}
df = pd.DataFrame(Employee_dictionary, columns = ['Emp_name', 'Emp_unique_id', 'Desgination', 'Emp_add'])
for row in df.itertuples(index = True, name ='Python'):
print (getattr(row, 'Emp_name'), getattr(row, "Emp_add"))
In the above Program, we create the DataFrame by using itertuples() and iterrows() method. Now we want to try to iterate the rows and columns of the Pandas DataFrame.
In the case of itertuple() example as the ‘index’ and ‘name’ argument of the DataFrame method is True and it will return the elements as values and names.
Here is the output of the following given code.
Read: Python convert DataFrame to list
Pandas DataFrame iterrows set value
- Here we can see how to set the value in Pandas DataFrame while using iterrows() method.
- By using the index position and iloc method we can set the value of rows in a DataFrame.
Example:
import pandas as pd
new_df = pd.DataFrame([[64, 118, 801],
[249, 321,963],
[136, 589, 501]],
index=[0,1,2],
columns=['m', 'n', 'o'])
print(pd.DataFrame(new_df))
new_df.loc[0, 'm'] = 812
new_df.loc[1, 'n'] = 678
new_df.loc[2, 'o'] = 512
print(pd.DataFrame(new_df))
In the above Program we have set the values for ‘m’, ‘n’, and ‘o’ and iterate over rows of a DataFrame by using the iloc and index method. Once you will print ‘new_df’ then the output will display the updated DataFrame.
You can refer to the below Screenshot
Read: Python dictionary increment value
Pandas DataFrame iterrows for loop
- In this Program we will discuss how to use for loop and iterrows method in Pandas DataFrame for iterating over rows and columns of DataFrame.
- In Pandas, the for loop method is generally used to iterate over the columns and rows of a DataFrame as tuple pairs.
- Let’s create a Pandas DataFrame and check how to apply this method in the Program. In the below code ‘df’ contains the information regarding student data ‘stu_name’ and ‘stu_id’ of four people. Now use the iterrows() function to get the iterated row.
Example:
import pandas as pd
new_stu_info = {'stu_name': ['Chris', 'oliva', 'Elite', 'jonas'],
'stu_id': [167,524,132,267]}
df = pd.DataFrame(new_stu_info, columns = ['stu_name','stu_id'])
for index, row in df.iterrows():
print (row['stu_name'], row['stu_id'])
Here is the execution of the following given code
Read: Python dictionary of lists
Pandas DataFrame iterrows change value
- Let us see how to modify the values which is available in the Pandas DataFrame.
- To do this particular task we acn apply the concept of iterrows() and get the updated value in DataFrame.
Source Code:
import pandas as pd
new_elements = [(102, 115, 721, 845) ,
(334, 578, 439, 120) ,
(290, 389, 164, 510)
]
df = pd.DataFrame(new_elements, columns=['Banana', 'Apple' , 'Oranges', 'Grapes'])
for new_ind, select_row in df.iterrows():
df.at[new_ind , 'Apple'] = select_row['Apple'] * 6
print(df)
In the above program, we have modified each value in Column ‘Apple’ by multiplying it with 6. Once you will print ‘df’ then the output will display in the form of an updated DataFrame.
Here is the implementation of the following given code
Related Posts:
In this tutorial, we have learned how to use the iterrows() function in Pandas DataFrame using Python. Also, we have covered these topics.
- Pandas DataFrame iterrows index
- Pandas DataFrame iterrows slow
- Pandas DataFrame iterrows update value
- Pandas DataFrame iterrows add column
- Pandas DataFrame iterrows reverse
- Pandas DataFrame iterrows skip first row
- Pandas series iterrows
- Pandas update DataFrame with iterrows
- Pandas iterrows return DataFrame
- Pandas DataFrame itertuples vs iterrows
- Pandas DataFrame iterrows set value
- Pandas DataFrame iterrows for loop
- Pandas DataFrame iterrows change value
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.