关于pandas中groupby的参数as_index的True与False
来源:互联网 发布:阿里云全民云计算活动 编辑:程序博客网 时间:2024/06/05 05:22
在完成作业的过程中遇到了一些困难,在参考别的同学代码中发现他比我多了一条as_index=False,就把index的标题位置上移,为实现后面的工作提供了基础。上面说的比较抽象,在下面有实例说明。
首先看一下pandas官方给出的groupby函数,可以看到默认值为as_index=True
grouby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, **kwargs)下面部分是从https://stackoverflow.com/questions/41236370/what-is-as-index-in-groupby-in-pandas搬运
import pandas as pddf = pd.DataFrame(data={'books':['bk1','bk1','bk1','bk2','bk2','bk3'], 'price': [12,12,12,15,15,17]})print dfprintprint df.groupby('books', as_index=True).sum()printprint df.groupby('books', as_index=False).sum()Output:
注意两次print输出中‘book’和‘price’的位置
books price0 bk1 121 bk1 122 bk1 123 bk2 154 bk2 155 bk3 17 pricebooks bk1 36bk2 30bk3 17 books price0 bk1 361 bk2 302 bk3 17
When as_index=True
the key(s) you use in groupby will become an index in the new dataframe.
The benefit of as_index=True
is that you can yank out the rows you want by using key names. For eg. if you want 'bk1'
you can get it like this: df.loc['bk1']
as opposed to when as_index=False
then you will have to get it like this: df.loc[df.books=='bk1']
Including the other main benefit of using as_index=True
raised by @ayhan in comments: df.loc['bk1']
would be faster because it doesn't have to traverse the entire books
column to find 'bk1'
when it's indexed. It will just calculate the hash value of 'bk1'
and find it in 1 go.
- 关于pandas中groupby的参数as_index的True与False
- true false 与TRUE FALSE 的区别
- FALSE/TRUE与false/true的区别
- FALSE/TRUE与false/true的区别
- FALSE/TRUE与false/true的区别
- FALSE/TRUE与false/true的区别
- 关于pandas.DataFrame的groupby的用法
- pandas 的groupby函数
- xmlHttp.open的第三个参数true与false
- pandas.DataFrame.drop_duplicates后面inplace=True与inplace=False的区别
- 909422229_Ajax中async的属性:true与false
- 理解pandas的groupby().apply()
- 简要说明python pandas中groupby,agg等的用法
- ajax中async true/false的作用
- JS中true和false的判断
- js中 [] ? true : false 的返回值为什么是true?
- 关于【true positive,false positive,true negative,false negative】的几个解释
- 有关request.getSession参数(true或者false)的含义
- QQ第三方登录与QQ分享
- phpstudy.bin在线安装
- MyBatis的二级缓存的设计原理,属于转载,内容很详细,值得学习。
- 关于JDK动态代理的小demo
- 【EXPDP/IMPDP】ORACLE数据泵导入导出案例(expdp & impdp)
- 关于pandas中groupby的参数as_index的True与False
- Uiautomator依赖jar包找不到的提示
- 使用Android Studio阅读AOSP源码
- 何时调用拷贝构造函数和赋值函数
- Linux 文件基本属性
- [FAQ12112]在电池低电压时,如何关闭camera的闪光功能
- 图像质量评价方法中客观评价方法又可以分为三类
- Python如何生成自带信息的二维码
- 假期归来急需鸡血