When you use as_index=False
, you indicate to groupby()
that you don't want to set the column ID as the index (duh!). When both implementation yield the same results, use as_index=False
because it will save you some typing and an unnecessary pandas operation ;)
However, sometimes, you want to apply more complicated operations on your groups. In those occasions, you might find out that one is more suited than the other.
Example 1: You want to sum the values of three variables (i.e. columns) in a group on both axes.
Using as_index=True
allows you to apply a sum over axis=1
without specifying the names of the columns, then summing the value over axis 0. When the operation is finished, you can use reset_index(drop=True/False)
to get the dataframe under the right form.
Example 2: You need to set a value for the group based on the columns in the groupby()
.
Setting as_index=False
allow you to check the condition on a common column and not on an index, which is often way easier.
At some point, you might come across KeyError
when applying operations on groups. In that case, it is often because you are trying to use a column in your aggregate function that is currently an index of your GroupBy object.