Skip to content
Advertisement

What is as_index in groupby in pandas?

What exactly is the function of as_index in groupby in Pandas?

Advertisement

Answer

print() is your friend when you don’t understand a thing. It clears out doubts many times.

Take a look:

JavaScript

Output:

JavaScript

When as_index=True the key(s) you use in groupby() will become an index in the new dataframe.

The benefits you get when you set the column as index are:

  1. Speed. When you filter values based on the index column eg. df.loc['bk1'], it would be faster because of hashing of index column. It doesn’t have to traverse the entire books column to find 'bk1'. It will just calculate the hash value of 'bk1' and find it in 1 go.

  2. Ease. When as_index=True you can use this syntax df.loc['bk1'] which is shorter and faster as opposed to df.loc[df.books=='bk1'] which is longer and slower.

Advertisement