pandas官方文档中cookbook(2)的split&building criteria翻译
来源:互联网 发布:福建公安便民网络 编辑:程序博客网 时间:2024/06/01 20:03
文档版本:0.20.3
这些例子是用python3.4写出来的。对于较早的python版本需要对代码做些相应的调整。
Pandas(pd)和Numpy(np)是唯一两个默认导入的包。其余的包会显示导入给新用户看。
若有翻译不当的地方,请多多指教。
这份文档中的例子都是从Stack-Overflow和Github中别人提问的比较经典的问题,作者从中提炼与总结。
分割数据框
用布尔值条件分割数据库In [9]: df = pd.DataFrame(...: {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df...: Out[9]: AAA BBB CCC0 4 10 1001 5 20 502 6 30 -303 7 40 -50In [10]: dflow = df[df.AAA <= 5]; dflowOut[10]: AAA BBB CCC0 4 10 1001 5 20 50In [11]: dfhigh = df[df.AAA > 5]; dfhighOut[11]: AAA BBB CCC2 6 30 -303 7 40 -50
建立复杂的布尔值条件
对多列进行布尔值判断来选取数据
In [12]: df = pd.DataFrame(....: {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df....: Out[12]: AAA BBB CCC0 4 10 1001 5 20 502 6 30 -303 7 40 -50
与逻辑(若无分配数值就返回一个Series)
In [13]: newseries = df.loc[(df['BBB'] < 25) & (df['CCC'] >= -40), 'AAA']; newseriesOut[13]: 0 41 5Name: AAA, dtype: int64
或逻辑(若有分配数据就返回一个新的数据框)
In [15]: df.loc[(df['BBB'] > 25) | (df['CCC'] >= 75), 'AAA'] = 0.1; dfOut[15]: AAA BBB CCC0 0.1 10 1001 5.0 20 502 0.1 30 -303 0.1 40 -50
选取最接近用于给定的一个数值的行通过使用argsort函数
In [16]: df = pd.DataFrame(....: {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df....: Out[16]: AAA BBB CCC0 4 10 1001 5 20 502 6 30 -303 7 40 -50In [17]: aValue = 43.0In [18]: df.loc[(df.CCC-aValue).abs().argsort()]Out[18]: AAA BBB CCC1 5 20 500 4 10 1002 6 30 -303 7 40 -50
通过使用二进制运算符动态减少判断列表
In [19]: df = pd.DataFrame(....: {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df....: Out[19]: AAA BBB CCC0 4 10 1001 5 20 502 6 30 -303 7 40 -50In [20]: Crit1 = df.AAA <= 5.5In [21]: Crit2 = df.BBB == 10.0In [22]: Crit3 = df.CCC > -40.0
一种硬编码的方式:
In [23]: AllCrit = Crit1 & Crit2 & Crit3
或者可以用动态建立判断列表的方法:
In [24]: CritList = [Crit1,Crit2,Crit3]In [25]: AllCrit = functools.reduce(lambda x,y: x & y, CritList)In [26]: df[AllCrit]Out[26]: AAA BBB CCC0 4 10 100
阅读全文
1 0
- pandas官方文档中cookbook(2)的split&building criteria翻译
- pandas官方文档cookbook(6)中Split&Pivot&Apply翻译
- pandas官方文档cookbook(5)中MissValue&groupby翻译
- pandas官方文档中cookbook(1)的idioms的翻译
- pandas官方文档中cookbook(3)的selection&new column&multiindexing翻译
- pandas官方文档cookbook(4)中Arithmetic&Slicing&Sorting翻译
- 十分钟掌握pandas(pandas官方文档翻译)
- 十分钟搞定pandas(官方学习文档的译文)
- CritterAI官方文档翻译【Building Multi-Tile Meshes】
- Android官方文档翻译 二 1.Building Your First App
- FMDB官方文档的翻译
- 对官方文档中NSAutoreleasePool的个人翻译
- fmdb官方文档(翻译)
- [置顶]android官方文档翻译(不断更新中。。。)
- 第一个简单的程序——Struts 2 官方文档翻译(Struts 2官方文档中文版)
- runspec 的选项说明(spec2006官方文档的翻译)
- Android Geofence的学习(一)翻译官方文档
- Android Geofence的学习(二)继续翻译官方文档
- hdu 2057 A + B Again
- spring cloud 之eureka-server
- 一些题
- 7.约束
- bzoj2434 阿狸的打字机 fail树+树状数组
- pandas官方文档中cookbook(2)的split&building criteria翻译
- 基于pycaffe从零开始写mnist(第三篇)——生成deploy.prototxt,做最后的验证
- 多种方法实现多态!!!
- WebSacrab与SQL注入
- [ifrog] Round #19 B -- Buildings
- 【笔试题】CVTE C++开发岗笔试题7.29
- POJ 1661 Help Jimmy(动态规划--最短下降模板)
- HTML仿抽屉新闻
- linux 下查找文件或者内容命令whereis,locate ,find