SUNDONG SUNDONG - 2 months ago 20
Python Question

Aggregate dataframe indices in python

I want to aggregate indices of a dataframe with groupby function.

word count
0 a 3
1 the 5
2 a 3
3 an 2
4 the 1


What I want is a pd.Series which consists of list(descending order) of indices,

word
a [2, 0]
an [3]
the [4, 1]


I've tried some built-in functions with groupby, however, I couldn't find a way to aggregate indices. Would you like to provide any hint or solution for this problem?

Answer

I think you can first change order of index by [::-1], then groupby and apply index to list. Last sort_index:

print (df[::-1].groupby('word', sort=False).apply(lambda x: x.index.tolist()).sort_index())
word
a      [2, 0]
an        [3]
the    [4, 1]
dtype: object

Another similar solution:

print (df.sort_index(ascending=False)
         .groupby('word', sort=False)
         .apply(lambda x: x.index.tolist())
         .sort_index())
word
a      [2, 0]
an        [3]
the    [4, 1]
dtype: object