Iterate over rows and columns in a Pandas DataFrame

In this lesson, we will learn how to iterate over rows and columns in a Pandas DataFrame. Let’s see examples of how to:

  1. Iterate over rows
  2. Iterate over columns

Iterate over rows

To iterate over the rows, use the following methods in Pandas:

  • iterrows(): To Iterate over the rows
  • itertuples(): To Iterate over the rows

Pandas iterrows() to iterate over rows

To iterate over rows, use the Python Pandas iterrows() method. Here, we are only displaying the student names from the student records. Let us see an example:

Output

Pandas itertuples() to iterate over rows

To iterate over rows, use the Python Pandas itertuples() method. Here, each row is returned as a Python Tuple object. Here, we are only displaying the student names from the student records

Let us see an example:

Output

Iterate over columns

To iterate over columns, use the following methods in Pandas:

  • items(): To Iterate over the columns
  • iteritems(): To Iterate over the columns (Deprecated now)

Pandas items() to iterate over columns

To iterate each and every column, use the Pandas items() method. The result will display a label object that is the name of the column and a column object that is what you have in the column.

Let us see an example to iterate over columns:

Output

Pandas iteritems() to iterate over columns

The Pandas itertems() method was deprecated. Use the items() as shown above to iterate over columns.


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


 

Delete rows/ columns in a Pandas DataFrame
Pandas - Date Time
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading