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.

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.

1. Quick Examples of Iterate Over Series

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


# Below are a quick example

# 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)

Yields below output.


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

Here is another example. 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)

9. 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 !!

Related Articles

References

Leave a Reply

You are currently viewing Pandas Iterate Over Series