函数pandas.DataFrame.groupby参数as_index的意义

本文深入探讨了Pandas库中DataFrame.groupby方法的as_index参数作用,通过实例对比as_index为True与False时对数据分组及显示的影响,揭示了设置as_index参数对查询速度和数据展示的不同效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数pandas.DataFrame.groupby参数as_index的意义

含义:as_index决定了分组使用的属性是否成为新的表格的索引,默认是as_index=True,我的代码中常用:as_index=False.

  • 使用作为索引只是会影响查询速度,而一般没有这样的需求。
  • as_index=True是常用的表格形式,而as_index=False除了表格有变化,显示也会不同。
文档 pandas.DataFrame.groupby
DataFrame.groupby(self, by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, 
squeeze=False, observed=False, **kwargs)[source]
  Group DataFrame or Series using a mapper or by a Series of columns.
  A groupby operation involves some combination of splitting the object, applying a function, and combining the results. This can be used to group large amounts of data and compute operations on these groups.
Parameters:
  sort : bool, default True
         Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. Groupby preserves the order of rows within each group.

参考 stackoverflow上“what-is-as-index-in-groupby-in-pandas”的回答,来举个例子:
创建一个表格,有group_id,age,status三个属性。

import pandas as pd
test = {"group_id":[1,1,2,3,3,3,4,4],"age":[22,15,27,35,28,17,45,29],
        "status":[1,2,3,4,5,6,7,8]}
df = pd.DataFrame(test)
df
group_idagestatus
0122
1115
2227
3335
4328
5317
6445
7429
df.groupby(['group_id']).mean()

as_index=True(默认)得到的是以group_id作为索引的DataFrame,这里我认为是在显示上索引名和属性名区分开,所以,group_id会比age和status低一点。在这里插入图片描述

df.groupby(['group_id'], as_index=False).mean()

as_index=False得到的表格就没有使用group_id作为索引。
在这里插入图片描述

补充使用双属性进行分组

df.groupby(['group_id','age']).mean()

as_index=True得到的是以group_id,age作为索引的DataFrame。
在这里插入图片描述

默认使用as_index=True的原因是将分组属性作为索引,这在之后的使用中能增加查询速度。
将属性A,B一起用于分组,也是同样的。

参考:

  1. https://stackoverflow.com/questions/41236370/what-is-as-index-in-groupby-in-pandas
  2. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html?highlight=groupby#pandas.DataFrame.groupby
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值