close

How to iterate over Pandas Series generated from groupby().size()

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to iterate over Pandas Series generated from groupby().size() in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to iterate over Pandas Series generated from groupby().size()?

  1. How to iterate over Pandas Series generated from groupby().size()?

    The last way is useful in case your index contains other information than the row number itself (e.g. in a case of a timeseries where the index is time).

  2. iterate over Pandas Series generated from groupby().size()

    The last way is useful in case your index contains other information than the row number itself (e.g. in a case of a timeseries where the index is time).

Method 1

Update:

Given a pandas Series:

s = pd.Series([1,2,3,4], index=['a', 'b', 'c', 'd'])

s
#a    1
#b    2
#c    3
#d    4
#dtype: int64

You can directly loop through it, which yield one value from the series in each iteration:

for i in s:
    print(i)
1
2
3
4

If you want to access the index at the same time, you can use either items or iteritems method, which produces a generator that contains both the index and value:

for i, v in s.items():
    print('index: ', i, 'value: ', v)
#index:  a value:  1
#index:  b value:  2
#index:  c value:  3
#index:  d value:  4

for i, v in s.iteritems():
    print('index: ', i, 'value: ', v)
#index:  a value:  1
#index:  b value:  2
#index:  c value:  3
#index:  d value:  4

Old Answer:

You can call iteritems() method on the Series:

for i, row in df.groupby('a').size().iteritems():
    print(i, row)

# 12 4
# 14 2

According to doc:

Series.iteritems()

Lazily iterate over (index, value) tuples

Method 2

To expand upon the answer of Psidom, there are three useful ways to unpack data from pd.Series. Having the same Series as Psidom:

s = pd.Series([1,2,3,4], index=['a', 'b', 'c', 'd'])

  • A direct loop over s yields the value of each row.
  • A loop over s.iteritems() or s.items() yields a tuple with the (index,value) pairs of each row.
  • Using enumerate() on s.iteritems() yields a nested tuple in the form of: (rownum,(index,value)).

The last way is useful in case your index contains other information than the row number itself (e.g. in a case of a timeseries where the index is time).

s = pd.Series([1,2,3,4], index=['a', 'b', 'c', 'd'])

for rownum,(indx,val) in enumerate(s.iteritems()):
    print('row number: ', rownum, 'index: ', indx, 'value: ', val)

will output:

row number:  0 index:  a value:  1
row number:  1 index:  b value:  2
row number:  2 index:  c value:  3
row number:  3 index:  d value:  4

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read