Looping through a Python pivot table

Question:

I have a pivot table that I have created (pivotTable) using:

pivotTable= dayData.pivot_table(index=['sector'], aggfunc='count')

which has produced the following pivot table:

                   sector id  
broad_sector                            
Communications         2   2 
Utilities              3   3
Media                  3   3

Is there a way to loop through the pivot table assigning the index value and sector total to respective variables sectorName and sectorCount?

I have tried:

i=0
while i <= lenPivotTable:
    sectorName = sectorPivot.index.get_level_values(0)
    sectorNumber = sectorPivot.index.get_level_values(1)
    i=i+1 

to return for the first loop iteration:

sectorName = 'Communications'
sectorCount = 2

for the second loop iteration:

sectorName = 'Utilities'
sectorCount = 3

for the third loop iteration:

sectorName = 'Media'
sectorCount = 3

But can’t get it to work.

Asked By: Stacey

||

Answers:

This snippet will get you the values as asked.

for sector_name, sector_count, _ in pivotTable.to_records():
    print(sector_name, sector_count)
Answered By: michael_j_ward

I don’t understand why do you need this (because looping through DF is very slow), but you can do it this way:

In [403]: for idx, row in pivotTable.iterrows():
   .....:         sectorName = idx
   .....:         sectorCount = row['sector']
   .....:         print(sectorName, sectorCount)
   .....:


Communications 2
Utilities 3
Media 3
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.