• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:14 mins read
You are currently viewing Pandas Iterate Over Series

Like any other data structure, Pandas Series also has a way to iterate (loop through) over rows and access elements of each row. You can use the for loop to iterate over the pandas Series.

Advertisements

You can also use multiple functions to iterate over a pandas Series like iteritems(), items() and enumerate() function. In this article, I will explain how to iterate rows of pandas Series using these functions with examples.

Key Points –

  • Pandas provides the iteritems() method to iterate over elements in a Series, yielding both index and value pairs.
  • Pandas encourages the use of vectorized operations for efficiency, allowing you to perform operations on entire Series without explicit iteration.
  • While iteritems() is available, using a simple for loop to iterate through a Series is also an option, providing direct access to values.
  • The apply() function provides a versatile way to apply a custom function to each element in a Series, offering flexibility without the need for manual iteration.
  • Prefer vectorized operations over iteration for better performance. Pandas is optimized for vectorized operations and should be leveraged whenever possible.

1. Quick Examples of Iterate Over Series

If you are in a hurry, below are some quick examples of how to iterate over series.


# Quick examples of iterate over series

# Example 1: Use iterate over index series
for indx in ser:
    print(indx)
    
# Example 2: Use Series.iteritems() function 
# To iterate over all the elements 
for indx in ser.iteritems():
    print(indx)

# Examples 3: Iterate over series 
# Using Series.items()
for indx, values in ser.items():
    print('index: ', indx, 'value: ', values)
    
# Examples 4: Iterate over series 
# Using Series.iteritems()
for indx, values in ser.iteritems():
    print('index: ', indx, 'value: ', values)
    
# Examples 5: Using enumerate()
for rownum,(indx,values) in enumerate(ser.iteritems()):
    print('row number: ', rownum, 'index: ', indx, 'value: ', values)

3. Create Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure available in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.

Now, let’s create pandas series using a list of values.


import pandas as pd
  
# Create the Series
ser = pd.Series([20000,25000,23000,28000,55000,23000,28000])
  
# Create the Index
index = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
  
# Set the index
ser.index = index
print(ser)

Yields below output.


# Output:
Java       20000
Spark      25000
PySpark    23000
Pandas     28000
NumPy      55000
Python     23000
Oracle     28000
dtype: int64

4. Pandas Iterate Over Series

One of the simple ways to access elements of the pandas Series is by using Python for loop. Here I will iterate the Series and get the values one by one and print it on the console. For examples.


# Use iterate over index series
for indx in ser:
    print(indx)

Yields below output.


# Output:
20000
25000
23000
28000
55000
23000
28000

5. Iterate Over All the Elements of Series using iteritems()

Series.iteritems() function is used to iterate all the elements in the given series object. The following is the syntax of the iteritems().


# Syntax of Series.iteritems
Series.iteritems()

The iteritems() function returns the iterable of tuples containing the indx, values from a Series.


# Iterate over all the elements using Series.iteritems()
for indx in ser.iteritems():
    print(indx)

In the above example, iteritems() is used to iterate over both the index and values of the ser Series, and for each iteration, it prints the index and the corresponding value. This example yields the below output.


# Output:
('Java', 20000)
('Spark', 25000)
('PySpark', 23000)
('Pandas', 28000)
('NumPy', 55000)
('Python', 23000)
('Oracle', 28000)

Here is another example. If you want to iterate over all the elements of a Pandas Series using iteritems(). I will leave this to you to run and explore the output.


# Iterate over series using Series.iteritems()   
for indx, values in ser.iteritems():
    print('index: ', indx, 'value: ', values)

6. Iterate Over Series Using & Series.items()

Pandas Series.items() function iterates over a Series and returns the iterable of tuples containing the indx, values from a Series. For E.x, for indx, values in ser.items():.


# Iterate over series using Series.items()
for indx, values in ser.items():
    print('index: ', indx, 'value: ', values)

Yields below output.


# Output:
index:  Java value:  20000
index:  Spark value:  25000
index:  PySpark value:  23000
index:  Pandas value:  28000
index:  NumPy value:  55000
index:  Python value:  23000
index:  Oracle value:  28000

7. Using enumerate()

You can also use enumerate() with Series then, apply for loop to get the values of series, in the form of rownum, indx and values.


# Using enumerate()
for rownum,(indx,values) in enumerate(ser.iteritems()):
    print('row number: ', rownum, 'index: ', indx, 'value: ', values)

Yields below output.


# Output:
row number:  0 index:  Java value:  20000
row number:  1 index:  Spark value:  25000
row number:  2 index:  PySpark value:  23000
row number:  3 index:  Pandas value:  28000
row number:  4 index:  NumPy value:  55000
row number:  5 index:  Python value:  23000
row number:  6 index:  Oracle value:  28000

8. Complete Example For Iterate Over Series


import pandas as pd
  
# Create the Series
ser = pd.Series([20000,25000,23000,28000,55000,23000,28000])
  
# Create the Index
index = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
  
# Set the index
ser.index = index
print(ser)

# Use iterate over index series
for indx in ser:
    print(indx)
    
# Use Series.iteritems() function to
# Iterate over all the elements 
for indx in ser.iteritems():
    print(indx)

# Iterate over series using Series.items()
for indx, values in ser.items():
    print('index: ', indx, 'value: ', values)
    
# Iterate over series using Series.iteritems()
for indx, values in ser.iteritems():
    print('index: ', indx, 'value: ', values)
    
# Using enumerate()
for rownum,(indx,values) in enumerate(ser.iteritems()):
    print('row number: ', rownum, 'index: ', indx, 'value: ', values)

Frequently Asked Questions on Pandas Iterate Over Series

How can I iterate over all elements in a Pandas Series?

To iterate over all elements in a Pandas Series, you can use various methods, such as using a for loop, the iteritems() method, or vectorized operations.

What’s the difference between iteritems() and items()?

Both methods provide index-value pairs, but iteritems() is more memory-efficient as it yields items one at a time, while items() returns a list of tuples representing the index-value pairs.

Can I iterate over only the values of a Series?

You can iterate over only the values of a Pandas Series using a simple for loop. For example, the for loop iterates directly over the values of the series_data Series, printing each value in each iteration. This approach is straightforward when you only need to access the values and don’t require the corresponding index.

Is there a performance difference between iterating and using vectorized operations?

vectorized operations are generally more efficient than explicit iteration, as Pandas is optimized for such operations.

When should I use iterrows() instead of iteritems()?

iterrows() is used for DataFrames, providing both index and row data. For Series, use iteritems() for better performance.

Conclusion

In this article, you have learned how to iterate over pandas Series using Series.iteritems(), Series.items() and enumerate() functions with examples.

Happy Learning !!