Anchit Bhagat — February 17, 2021
Beginner Data Exploration Python

This article was published as a part of the Data Science Blogathon.

Introduction

An iteration is an object that repeats identical or similar tasks without making errors. In a way, we can say repeated execution of a set of statements is what iteration is all about. Python has several language features to make it easier to perform the iteration task.

As an object, the iterator counts a number of values that can be iterated upon. Lists, tuples, dictionaries, strings, and sets are all iterable objects. They are iterable containers from which you can get an iterator from.

In the following topic, we’ll see a brief of different processes of iteration.

 

Looping using enumerate()

Using a for loop to iterate over a list only gives us access to every list element in each run, one after the other. If one also wants to access the index information, so where the list element we are iterating over is located, we can use enumerate().

As an example, have a look at how the for loop was converted by creating an areas list:

# areas list
areas = [11.25, 18.0, 20.0, 10.75, 9.50]

# Change for loop to use enumerate() and update print()
for x, y in enumerate(areas) :
    print("room ", str(x), ": ", str(y))

Output:

room 0: 11.20
room 1: 18.0
room 2: 20.0
room 3: 10.75
room 4: 9.5

Loop over dictionary – items()

In Python 3, we need the items() method to loop over a dictionary. On each iteration, "the capital of x is y" will be printed out, where x is the key and y is the value of the pair.
# Definition of dictionary
europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin',
 'norway':'oslo', 'italy':'rome', 'poland':'warsaw', 'austria':'vienna' }

# Iterate over europe
for x, y in europe.items():
    print("the capital of ", str(x), " is ", str(y))

Output:

the capital of norway is oslo
the capital of poland is warsaw
the capital of italy is rome
the capital of spain is madrid
the capital of austria is vienna
the capital of germany is berlin

Loop over Numpy array – np.nditer()

If we’re dealing with a 1D Numpy array, looping over all elements can be as simple as:

for x in my_array :

If we’re dealing with a 2D Numpy array, it’s more complicated. A 2D array is built up of multiple 1D arrays. To explicitly iterate over all separate elements of a multi-dimensional array, we’ll need this syntax:

for x in np.nditer(my_array) :

Below we are writing a for loop that iterates over all elements in np_height and prints out “x inches” for each element, where x is the value in the array.

# Import numpy as np
import numpy as np

# For loop over np_height
for x in np_height:
    print(x, "inches")

# For loop over np_baseball
for n in np.nditer(np_baseball):
    print(n)

Output:

74 inches
74 inches
72 inches
72 inches
73 inches
69 inches
69 inches
71 inches
76 inches
71 inches
73 inches…..

Looping through iterrows():

Using iterrows() to iterate over every observation of a Pandas DataFrame. Here, we are using a for loop to add a new column, named COUNTRY, that contains an uppercase version of the country names in the “country” column. We are using the string method upper() for this.

# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Code for loop that adds COUNTRY column
for lab, row in cars.iterrows():
    cars.loc[lab, "COUNTRY"] = row['country'].upper()

# Print cars
print(cars)

Using iterrows() to iterate over every observation of a Pandas DataFrame is easy to understand, but not very efficient. On every iteration, we are creating a new Pandas Series in Python. If we want to add a column to a DataFrame by calling a function on another column, the iterrows() method in combination with a for loop is not the preferred way to go. Instead, we’ll want to use apply()

Below we’ll use the apply() version to get the same result in the DataFrame:

# Use .apply(str.upper)
cars["COUNTRY"] = cars["country"].apply(str.upper)
print(cars)

Output:

cars_per_cap country drives_right (US, COUNTRY)
US 809 United States True UNITED STATES
AUS 731 Australia False AUSTRALIA
JPN 588 Japan False JAPAN
IN 18 India False INDIA
RU 200 Russia True RUSSIA
MOR 70 Morocco True MOR

We can utilize the above itertools to work with the iteration in python in a more effective way. This was just the brief on iteration. One can work over it with different examples.

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.

About the Author

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

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