2

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?

2
  • So you drop the tag and count columns? Commented Sep 22, 2016 at 12:17
  • @IanS Well yes. For this question, those columns are useless. I just put it to emphasize the data is a dataframe. Commented Sep 22, 2016 at 12:20

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I need to get used to lambda function!
Glad can help you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.