-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
QST: Related to working of as_index
in pandas groupby
#40591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Possibly, the issue is that (Personally, I always call agg = (
df.assign(multi5 = np.floor_divide(df.index, 5)) # functional form of df.index//5
.groupby(['multi5', 'A'], as_index=False)
.mean()
)
agg # below output to differ w/o np.random.seed(###)
|
Thanks @ninjakx for the report! From the groupby userguide docs:
Perhaps this could be added to the API docs as well. |
The full note in the documentation says:
I find this not very clear. Also the part about |
And regardless of the (unclear) documentation, I also don't understand the rationale of this behaviour if it is intentional. Why wouldn't we always make the group values a column with |
Closing in favor of #49519 |
I have searched the [pandas] tag on StackOverflow for similar questions.
I have asked my usage related question on StackOverflow.
Question about pandas
This is my df:
If I do
df.groupby([df.index//5,df.A], as_index=True).mean()
or
df.groupby([df.index//5,df.A]).mean()
Result:
So If do
.reset_index()
I will get this (that index with no column name will get name as
level_0
):The same is not with the
as_index=False
df.groupby([df.index//5,df.A], as_index=False).mean()
will give meI will lose the
groupby index info
.My question:
shouldn't be
as_index=False
givedf.index//5
column also. Oras_index
is designed to work like this only.The text was updated successfully, but these errors were encountered: