pandas - Looping through a python pivot table -


i have pivot table have created (pivottable) using:

pivottable= daydata.pivot_table(index=['sector'], aggfunc='count') 

which has produced following pivot table:

                   sector id   broad_sector                             communications         2   2  utilities              3   3 media                  3   3 

could let me know if there way loop through pivot table assigning index value , sector total respective variables sectorname , sectorcount

i have tried:

i=0 while <= lenpivottable:     sectorname = sectorpivot.index.get_level_values(0)     sectornumber = sectorpivot.index.get_level_values(1)     i=i+1  

to return first loop iteration:

sectorname = 'communications' sectorcount = 2 

for second loop iteration:

sectorname = 'utilities' sectorcount = 3 

for third loop iteration:

sectorname = 'media' sectorcount = 3 

but can't work. assistance appreciated

well, don't understand why need (because looping through df very slow), can way:

in [403]: idx, row in pivottable.iterrows():    .....:         sectorname = idx    .....:         sectorcount = row['sector']    .....:         print(sectorname, sectorcount)    .....:   communications 2 utilities 3 media 3 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -