在官方网站中对as_index有以下介绍:
as_index : boolean, default True
For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped output
翻译过来就是说as_index 的默认值为True, 对于聚合输出,返回以组标签作为索引的对象。仅与DataFrame输入相关。as_index = False实际上是“SQL风格”的分组输出。举例如下

输出如下:

代码中注释的两段代码报错,分析可以看到:
当as_index=True时,没有显示索引项,而是以第一列组标签为索引值,故不能通过df.loc[0]取值,可以通过df.loc[‘bk1’]取值;
当as_index=False时,显示索引项,此时可以通过df.loc[0]取得值。因此as_index的作用是控制聚合输出是否以组标签为索引值。
网友评论