TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas DataFrame.iterrows()

Pandas DataFrame.iterrows() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas DataFrame.iterrows()

If you want to loop over the DataFrame for performing some operations on each of the rows then you can use iterrows() function in Pandas.

Pandas use three functions for iterating over the rows of the DataFrame, i.e., iterrows(), iteritems() and itertuples().

Iterate rows with Pandas iterrows:

The iterrows () is responsible for loop through each row of the DataFrame. It returns an iterator that contains index and data of each row as a Series.

We have the next function to see the content of the iterator.

This function returns each index value along with a series that contain the data in each row.

  • iterrows() - used for iterating over the rows as (index, series) pairs.
  • iteritems() - used for iterating over the (key, value) pairs.
  • itertuples() - used for iterating over the rows as namedtuples.

Yields:

  • index: Returns the index of the row and a tuple for the MultiIndex.
  • data: Returns the data of the row as a Series.
  • it: Returns a generator that iterates over the rows of the frame.

Example1

import pandas as pd
import numpy as np

info = pd.DataFrame(np.random.randn(4,2),columns = ['col1','col2'])
for row_index,row in info.iterrows():
   print (row_index,row)

Output

0   name        John
     degree      B.Tech
score         90
Name: 0, dtype: object

1 name      Smith
degree    B.Com
score        40
Name: 1, dtype: object

2 name      Alexander
degree        M.Com
score            80
Name: 2, dtype: object

3 name      William
degree     M.Tech
score          98
Name: 3, dtype: object

Example2

# importing pandas module  
import pandas as pd  
     
# making data frame from csv file  
data = pd.read_csv("aa.csv")  
  
for i, j in data.iterrows(): 
    print(i, j) 
    print()

Output

0           Name                  Hire Date     Salary            Leaves Remaining    0   John Idle                  03/15/14     50...
Name: 0, dtype: object

1           Name                  Hire Date     Salary            Leaves Remaining    1     Smith Gilliam        06/01/15      65000...
Name: 1, dtype: object

2           Name                  Hire Date     Salary            Leaves Remaining    2     Parker Chapman   05/12/14      45000.0   ...
Name: 2, dtype: object

3           Name                  Hire Date     Salary            Leaves Remaining    3     Jones Palin             11/01/13     700...
Name: 3, dtype: object

4           Name                  Hire Date     Salary            Leaves Remaining    4     Terry Gilliam          08/12/14     4800...
Name: 4, dtype: object

5           Name                  Hire Date     Salary            Leaves Remaining    5     Michael Palin         05/23/13     66000...
Name: 5, dtype: object

Next TopicDataFrame.join()




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf