I have a Pandas Series with multiple index that I'm trying to iterate over by the level "ID". The idea is the for loop will increment to the next "ID" so I can slice all the values associated with that ID to pass to a function for plotting each ID as a different color.
rest confidence
ID ts
33 21:30:50 150.01001 95.9864
21:30:52 148.826187 79.530624
21:30:53 148.957123 54.75795
55 21:30:52 168.325577 37.43358
21:30:53 172.813446 33.133442
61 21:30:50 107.335625 32.807873
The Pandas doc (Pandas MultiIndex) has been helpful with slicing and getting a working for loop (below). Using df.index.levels[0] returns the index values I need to run the for loop, however, it seems like there's a better and faster way to tell it to iterate over a given index level. Is there?
for IDn in list(df.index.levels[0]):
print( df.loc[ (IDn,slice(None)),['confidence','rest'] ].xs(slice(None),level='ID') )
I've gone through this question some (Pandas how to loop through a MultiIndex series) and it seems the groupby and apply function are the way.