Iteration

Iteration means to take each item of something one after another. As pandas dataframe consist of rows and columns so to iterate over it we have to use iteration method as used in a dictionary that is, iterating over key and value pair. where the key would be a column name and value would be its entries.

The behavior of basic iteration over Pandas objects depends on the type. When iterating over a Series, it is regarded as array-like, and basic iteration produces the values. Other data structures, like DataFrame and Panel, follow the dict-like convention of iterating over the keys of the objects.

In short, basic iteration (for i in object) produces −

  • Series − values
  • DataFrame − column labels
  • Panel − item labels

Before we go on any further let us clear two main definitions

iterator – The one which is used to iterate over an iterable.

iterable – The one which can be iterated.

Iterating Dataframe

Iterating a dataframe gives the column name as shown in the example below.

To iterate over the rows we can use 3 functions

  • iteritems() :  to iterate over the (key,value) pairs
  • itertuples() :  iterate over the rows as namedtuples
  • iterrows() :  to iterate over the (key,value) pairs

iteritems()

iteritems is used to iterate over the key-value pair that is it will iterate over each column and each row.

as we can see each value is separated as a key-value pair.

iterrows()

It is used to iterate over the rows that means, it will go on iterating each index and give the value of each column along with the column name.

Note − Because iterrows() iterate over the rows, it doesn’t preserve the data type across the row. 0,1,2 are the row indices and first and second are column indices.

itertuples()

itertuples() method will return a named tuple for each row in the DataFrame. The first element of the tuple will be the row’s corresponding index value, while the remaining values are the row values.

as we can see here we need only one iterator because at each iteration we get iterable.

Note − Do not try to modify any object while iterating. Iterating is meant for reading and the iterator returns a copy of the original object (a view), thus the changes will not reflect on the original object.

Iteration over columns

Columns can be iterated just like an array with a single iterator.

Close Menu