Difference between “as_index = False”, and “reset_index()” in pandas groupby

自作多情 提交于 2019-12-23 12:07:43

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/51866908/difference-between-as-index-false-and-reset-index-in-pandas-groupby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!