18

I just wanted to know what is the difference in the function performed by these 2.

Data:

import pandas as pd
df = pd.DataFrame({"ID":["A","B","A","C","A","A","C","B"], "value":[1,2,4,3,6,7,3,4]})

as_index=False :

df_group1 = df.groupby("ID").sum().reset_index()

reset_index() :

df_group2 = df.groupby("ID", as_index=False).sum()

Both of them give the exact same output.

  ID  value
0  A     18
1  B      6
2  C      6

Can anyone tell me what is the difference and any example illustrating the same?

jpp
  • 159,742
  • 34
  • 281
  • 339
Rohith
  • 1,008
  • 3
  • 8
  • 19
  • They are exactly the same. – Qusai Alothman Aug 16 '18 at 14:29
  • 1
    @QusaiAlothman: No, they're only the same in this particular case **because the OP's dataframe doesn't have an explicit index other than the default one 0,1,2...** so keeping it or resetting/dropping it doesn't make a difference. If the dataframe actually had an index e.g. 100, 101, 102.. the results would not be the same. – smci Sep 23 '19 at 17:09

1 Answers1

29

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.

usr
  • 782
  • 1
  • 7
  • 25
qmeeus
  • 2,341
  • 2
  • 12
  • 21