My question is similar to "grouping rows in list in pandas groupby", but what is to be listified is the index, not just another column.
I know I can turn the index into just another column with reset_index()
, but I spent a lot of time trying to capture the index field directly. Is there a way?
Example:
df = pd.DataFrame({'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6]})
df.reset_index().groupby('a')['index'].apply(list)
Output:
A [0, 1]
B [2, 3, 4]
C [5]