Aggregate dataframe indices in python
<p>I want to aggregate indices of a dataframe with groupby function.  </p>

<pre><code>     word  count
0    a     3
1    the   5
2    a     3
3    an    2
4    the   1
</code></pre>

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

<pre><code>word
a       [2, 0]
an         [3]
the     [4, 1]
</code></pre>

<p>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?</p>

<p>I think you can first change order of <code>index</code> by <code>[::-1]</code>, then <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html" rel="nofollow"><code>groupby</code></a> and <code>apply</code> <code>index</code> to <code>list</code>. Last <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.sort_index.html" rel="nofollow"><code>sort_index</code></a>:</p>

<pre><code>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
</code></pre>

<p>Another similar solution:</p>

<pre><code>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
</code></pre>

python pandas series