How to reset a DataFrame’s indexes for all groups in one step?

Pass in as_index=False to the groupby, then you don’t need to reset_index to make the groupby-d columns columns again:

In [11]: grouped = df.groupby('A', as_index=False)

In [12]: grouped.get_group('foo')
Out[12]:
     A  B
0  foo  1
2  foo  3
4  foo  5
6  foo  7
7  foo  8

Note: As pointed out (and seen in the above example) the index above is not [0, 1, 2, ...], I claim that this will never matter in practice – if it does you’re going to have to just through some strange hoops – it’s going to be more verbose, less readable and less efficient…

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)