Iterate DataFrame in Python

Iterate DataFrame in Python

In recent time, data analytics is the most demanding area in IT. Panda is a library which is commonly used for analytic using Python programming language. In Panda, Series and DataFrame are the data structures that we commonly use and because of these data structures, Panda is so popular. In this article, we will be discussing different ways to iterate DataFrame.

1) Iterate DataFrame row by row using iterrows()

iterrows() method returns a Series for each row along with index. The iterator returns a copy and not a view and writing to it will have no effect. So, one shouldn’t modify while iterating the DataFrame using iterrow(). Below are the examples to demonstrate how to iterate DataFrame using iterrow() method.

from pandas import DataFrame
if __name__ == '__main__':
    employees = [{'firstName': 'Rahul', 'lastName': 'Anand', 'age':26},
                 {'firstName': 'Sandeep', 'lastName': 'Kulkarni', 'age':25},
                 {'firstName': 'Daniel', 'lastName': 'Charles', 'age':32}]
    df = DataFrame(employees);
    
    #iterate all rows
    for index, row in df.iterrows() :
        print('Data at index', index)
        print(row)
        
    #get specific column(firstName)
    print('\n*** FirstName ***')
    for index, row in df.iteritems() :
        print(row[1])

Output

Data at index 0
age             26
firstName    Rahul
lastName     Anand
Name: 0, dtype: object
Data at index 1
age                25
firstName     Sandeep
lastName     Kulkarni
Name: 1, dtype: object
Data at index 2
age               32
firstName     Daniel
lastName     Charles
Name: 2, dtype: object

*** FirstName ***
25
Sandeep
Kulkarni

2) Iterate DataFrame row by row using itertuples()

Iterate over DataFrame rows as namedtuples, with index values as first element of the tuple. Below example demonstrate iteration of DataFrame using itertuples().

from pandas import DataFrame
if __name__ == '__main__':
    employees = [{'firstName': 'Rahul', 'lastName': 'Anand', 'age':26},
                 {'firstName': 'Sandeep', 'lastName': 'Kulkarni', 'age':25},
                 {'firstName': 'Daniel', 'lastName': 'Charles', 'age':32}]
    df = DataFrame(employees);  

    #iterate DataFrame row by row
    for row in df.itertuples(index=True, name='Employee'):
        print(row)
        
    #get specific column(firstName)
    print('\n*** FirstName ***')
    for row in df.itertuples(index=True, name='Employee'):
        print(getattr(row, 'firstName'))

Output

Employee(Index=0, age=26, firstName='Rahul', lastName='Anand')
Employee(Index=1, age=25, firstName='Sandeep', lastName='Kulkarni')
Employee(Index=2, age=32, firstName='Daniel', lastName='Charles')

*** FirstName ***
Rahul
Sandeep
Daniel

Stay tuned for more updates and tutorials !!!

Leave a Reply

Your email address will not be published. Required fields are marked *