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文档

import numpy as np
arr = np.random.randn(4,4)
print(arr)
print(np.where(arr>0,2,-2))
print(np.where(arr>0,2,arr))

 

pandas 中的DataFrame.where()使用的更多相关文章

  1. python数据分析pandas中的DataFrame数据清洗

    pandas中的DataFrame中的空数据处理方法: 方法一:直接删除 1.查看行或列是否有空格(以下的df为DataFrame类型,axis=0,代表列,axis=1代表行,以下的返回值都是行或列 ...

  2. pandas中遍历dataframe的每一个元素

    假如有一个需求场景需要遍历一个csv或excel中的每一个元素,判断这个元素是否含有某个关键字 那么可以用python的pandas库来实现. 方法一: pandas的dataframe有一个很好用的 ...

  3. Pandas中的DataFrame按指定顺序输出所有列的方法

    问题: 输出新建的DataFrame对象时,DataFrame中各列的显示顺序和DataFrame定义中的顺序不一致. 例如: import pandas as pd grades = [48,99, ...

  4. 【Python学习】解决pandas中打印DataFrame行列显示不全的问题

    在使用pandas的DataFrame打印时,如果表太长或者太宽会自动只给前后一些行列,但有时候因为一些需要,可能想看到所有的行列. 所以只需要加一下的代码就行了. #显示所有列 pd.set_opt ...

  5. pandas中,dataframe 进行数据合并-pd.concat()

    ``# 通过数据框列向(左右)合并 a = pd.DataFrame(X_train) b = pd.DataFrame(y_train) # 合并数据框(合并前需要将数据设置成DataFrame格式 ...

  6. pandas中关于DataFrame 去除省略号

    #显示所有列 pd.set_option('display.max_columns', None) #显示所有行 pd.set_option('display.max_rows', None) #设置 ...

  7. Pandas数据帧(DataFrame)

    数据帧(DataFrame)是二维数据结构,即数据以行和列的表格方式排列. 数据帧(DataFrame)的功能特点: 潜在的列是不同的类型 大小可变 标记轴(行和列) 可以对行和列执行算术运算 结构体 ...

  8. python – 基于pandas中的列中的值从DataFrame中选择行

    如何从基于pandas中某些列的值的DataFrame中选择行?在SQL中我将使用: select * from table where colume_name = some_value. 我试图看看 ...

  9. Python之Pandas中Series、DataFrame

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

随机推荐

  1. redis 的hash数据类型

    hash的常用命令 1.hset hset key field value 将哈希表key中的域field的值设为value 如果key不存在,一个新的哈希表被创建并进行HSET操作 如果field是 ...

  2. CorelDRAW中关于锁定与解锁对象的操作

    在编辑复制的图形时,有时为了避免对象受到操作的影响,可以使用“锁定与解锁对象”功能键对已经编辑好的对象进行锁定.被锁定的对象将不能进行任何编辑操作,本教程将详解CorelDRAW中关于锁定与解锁对象的 ...

  3. 使用monkey技术修改python requests模块

    例如请求前和请求后各来一条日志,这样就不需要在自己的每个代码都去加日志了. 其实也可以直接记录'urllib3.connectionpool'  logger name的日志. 修改了requests ...

  4. Eclipse------使用Maven install出错:编码GBK的不可映射字符

    使用Maven install时报错:编码GBK的不可映射字符 原因:Maven默认使用GBK进行编码 解决方法: 在pom.xml文件中添加如下代码即可 <project> <pr ...

  5. JS有趣的单线程

    一.JS的执行特点    源于单线程的特性, JS在一段时间内只能执行一部分代码, 那么, 当有多块代码需要执行时, 就需要排队等候了.   二.单线程与异步事件 (1) 什么是异步事件?     异 ...

  6. ios开发之--/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby: bad interpreter: No such file

    有一段时间没有用pod了,突然报了个这种错误,查了下,原来是没有更新pod, 1,更新gem:sudo gem update --system 2,查看gem源是否是最新的:gem sources - ...

  7. c 编译和链接过程

    详解link  有 些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错 ...

  8. Git 学习笔记--3.EGit使用手册

    zz http://blog.csdn.net/pandakong/article/details/7234974 EGit是Eclipse上的Git插件,官方内容参看http://wiki.ecli ...

  9. (转)作为一个新人,怎样学习嵌入式Linux?(韦东山)

    被问过太多次,特写这篇文章来回答一下.   在学习嵌入式Linux之前,肯定要有C语言基础.汇编基础有没有无所谓(就那么几条汇编指令,用到了一看就会).C语言要学到什么程度呢?越熟当然越好,不熟的话也 ...

  10. Android应用程序的结构

    1.src目录 存放该项目的源代码 2.gen目录 该目录文件是ADT自动生成的,并不需要认为地去修改 3.Android2.1 该目录存放的是该项目支持的JAR包,同时还包含项目打包时需要的META ...