How to Iterate Over a Series in Pandas


Often you may want to iterate over a pandas Series and perform some action on each item in the Series.

You can use the following methods to do so:

Method 1: Print Each Value in Series

for i in my_series:
    print(i)

This particular example will simply print each value in a pandas Series.

Method 2: Perform Some Action & Print Each Value in Series

for i in my_series:
    print(i*2)

This particular example will multiply each value in the pandas Series and then print it.

Method 3: Print Each Index and Each Value in Series

for i, j in my_series.items():
    print('Index:', i, 'Value:', j)

This particular example will print both the index and the value of each item in the pandas Series.

The following examples show how to use each method in practice with the following pandas Series:

import pandas as pd

#create pandas Series
my_series = pd.Series([1, 2, 3, 4, 5, 6], index=['A', 'B', 'C', 'D', 'E', 'F'])

#view Series
print(my_series)

A    1
B    2
C    3
D    4
E    5
F    6
dtype: int64

Example 1: Print Each Value in Series

The following code shows how to iterate over each value in a pandas Series and simply print each value:

for i in my_series:
    print(i)

1
2
3
4
5
6

By using a for loop with a print statement, we are able to iterate over each value in the series and print it.

Example 2: Perform Some Action & Print Each Value in Series

The following code shows how to iterate over each value in a pandas Series, multiply each value by 2, and then print the result:

for i in my_series:
    print(i*2)

2
4
6
8
10
12

Notice that each value in the original pandas Series has been multiplied by 2 and the result of each multiplication is shown for each value.

Also note that you can perform more complex operations on each value if you would like.

For example, we could use the following syntax to square each item in the Series and then divide it by 4:

for i in my_series:
    print(i**2 / 4)

0.25
1.0
2.25
4.0
6.25
9.0

Feel free to perform as complex of calculations as you would like on each item in the Series by using similar syntax.

Example 3: Print Each Index and Each Value in Series

Sometimes you may want to iterate over each item in a pandas Series and print both the index and the actual value.

The easiest way to do so is by using the pandas Series.items() method.

We can use the following syntax to print both the index and value of each item in the pandas Series:

for i, j in my_series.items():
    print('Index:', i, 'Value:', j)

Index: A Value: 1
Index: B Value: 2
Index: C Value: 3
Index: D Value: 4
Index: E Value: 5
Index: F Value: 6

Notice that the output shows both the index and the actual value for each item in the Series.

Note that you can use whatever syntax you’d like within the print statement to display both values. We chose to use “Index” and “Value” for simplicity.

You can find the complete documentation for the Series.items() method in Pandas here.

Additional Resources

The following tutorials explain how to perform other common tasks in Pandas:

How to Plot a Pandas Series
How to Convert Pandas Series to DataFrame
How to Convert Pandas Series to NumPy Array

Featured Posts

Leave a Reply

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