1

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]
bongbang
  • 1,610
  • 4
  • 18
  • 34
  • `df.index.groupby(df['a']).apply(list)`? – Quang Hoang Feb 20 '20 at 22:39
  • 1
    Is the list totally necessary, or just any collection? The simplest is `df.groupby('a').groups`. Not like you gain much with a DataFrame of list objects over a `dict` of Index objects – ALollz Feb 20 '20 at 23:17
  • 1
    @ALollz Wow, thank you for seeing the intent behind the question. No, a list is not absolutely necessary. I just needed something that would work with `np.random.choice`, and to my surprise, the `Int64Index` object totally does. Your solution is the one that I will implement, even though the question as posted requires a different one. – bongbang Feb 20 '20 at 23:33
  • Ah glad to help :D. The `groups` attribute is one of those random things that's hard to find, but useful when you need it. :D – ALollz Feb 21 '20 at 00:07

2 Answers2

1

Use a list comprehension to iterate through the groups, and create a new dataframe

(pd.DataFrame([(name,group.index.tolist())
                for name, group in df.groupby('a')],
                columns=['name','index'])
 )

    name    index
0   A   [0, 1]
1   B   [2, 3, 4]
2   C   [5]
sammywemmy
  • 27,093
  • 4
  • 17
  • 31
1

Try:

df.index.to_series().groupby(df['a']).apply(list)

Output:

a
A       [0, 1]
B    [2, 3, 4]
C          [5]
dtype: object
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • 1
    That's neat, although in fairness `df.index.to_series()` is rather like `df.reset_index()`. I suppose the indices (keys) must be turned into values somehow. – bongbang Feb 20 '20 at 22:51