pandas.DataFrame.where和mask 解读
1.前言背景
没怎么用过df.where 都是直接使用loc、apply等方法去解决。
可能是某些功能还没有超出loc和apply的适用范围。
2.进入df.where和df.mask
DataFrame.where(self, cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)
note:Replace values in DataFrame with other where the cond is False.
我们还是要看一下官网对里面每一个参数的解释:
红色是特别注意的,往往无论是博客还是案例一般给不会穷举所有可能,只有把api的每一种可能理解了,才能无招胜有招。
大体意思:就是对一个DataFrame进行条件判断当他的条件不符合就选择other参数里面的数值。
其实它拥有一个相反的函数where<==>mask:where条件不符合进行替换,mask是条件符合进行替换。
DataFrame.mask(self, cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)
note:Replace values in DataFrame with other where the cond is True.
我们还是要看一下官网对里面每一个参数的解释:
也可以看到两者参数并无差异。
3.与np.where的异同?
np.where(condition, [x, y]),这里三个参数,其中必写参数是condition(判断条件),后边的x和y是可选参数.那么这三个参数都有怎样的要求呢?
condition:array_like,bool ,当为True时,产生x,否则产生y
简单说,对第一个参数的要求是这样的,首先是数据类型的要求,类似于数组或者布尔值,当判断条件为真时返回x中的值,否则返回y中的值
x,y:array_like,可选,要从中选择的值。 x,y和condition需要可广播到某种形状
x和y是可选参数,并且对这两个参数的数据类型要求只有类似数组这一条,当条件判断为true或者false时从这两个类似数组的容器中取数.
4.实际案例
4.1mask和where 的区别,np.where(cond,df1,df2)
s = pd.Series(range(5))
s.mask(s > 0)
s.where(s > 0)
ss = pd.Series(range(10,20,2))
import numpy as np
np.where(s>2,s,ss)
4.2探究cond : boolean Series/DataFrame, array-like, or callable和other : scalar, Series/DataFrame, or callable
下面我在cond使用callable类型,在other参数中使用callable参数
df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
df
def cond1(x):
return x%3==0
def mult3(x):
return x*3
df.where(cond1, mult3)
pandas.DataFrame.where和mask 解读的更多相关文章
- pandas.DataFrame学习系列1——定义及属性
定义: DataFrame是二维的.大小可变的.成分混合的.具有标签化坐标轴(行和列)的表数据结构.基于行和列标签进行计算.可以被看作是为序列对象(Series)提供的类似字典的一个容器,是panda ...
- pandas.DataFrame的pivot()和unstack()实现行转列
示例: 有如下表需要进行行转列: 代码如下: # -*- coding:utf-8 -*- import pandas as pd import MySQLdb from warnings impor ...
- pandas DataFrame apply()函数(1)
之前已经写过pandas DataFrame applymap()函数 还有pandas数组(pandas Series)-(5)apply方法自定义函数 pandas DataFrame 的 app ...
- pandas DataFrame apply()函数(2)
上一篇pandas DataFrame apply()函数(1)说了如何通过apply函数对DataFrame进行转换,得到一个新的DataFrame. 这篇介绍DataFrame apply()函数 ...
- 把pandas dataframe转为list方法
把pandas dataframe转为list方法 先用numpy的 array() 转为ndarray类型,再用tolist()函数转为list
- pandas DataFrame.shift()函数
pandas DataFrame.shift()函数可以把数据移动指定的位数 period参数指定移动的步幅,可以为正为负.axis指定移动的轴,1为行,0为列. eg: 有这样一个DataFrame ...
- pandas DataFrame applymap()函数
pandas DataFrame的 applymap() 函数可以对DataFrame里的每个值进行处理,然后返回一个新的DataFrame: import pandas as pd df = pd. ...
- pandas DataFrame(3)-轴
和numpy数组(5)-二维数组的轴一样,pandas DataFrame也有轴的概念,决定了方法是对行应用还是对列应用: 以下面这个数据为例说明: 这个数据是5个车站10天内的客流数据: rider ...
- pandas DataFrame(4)-向量化运算
pandas DataFrame进行向量化运算时,是根据行和列的索引值进行计算的,而不是行和列的位置: 1. 行和列索引一致: import pandas as pd df1 = pd.DataFra ...
随机推荐
- fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC sha
调试程序时出现以下问题:d:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afx.h(24): fatal e ...
- mac必装软件
1.IINA: https://iina.io/ 2.keka: https://www.keka.io/zh-cn/ 3.欧陆词典: https://www.eudic.net/v4/en/app/ ...
- 《C++ Primer》读书笔记之第15章:面向对象编程
一.面向对象概述 1. 面向对象的三个基本特性 封装.继承和多态. 2. 封装 指把隐藏对象的实现细节,仅对外提供接口,从而达到接口与实现分离的效果.封装的好处:一是提高数据的安全性,用户只能使用对象 ...
- (模板)poj2947(高斯消元法解同余方程组)
题目链接:https://vjudge.net/problem/POJ-2947 题意:转换题意后就是已知m个同余方程,求n个变量. 思路: 值得学习的是这个模板里消元用到lcm的那一块.注意题目输出 ...
- PTA(Advanced Level)1033.To Fill or Not to Fill
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...
- 【思维】Kenken Race
题目描述 There are N squares arranged in a row, numbered 1,2,...,N from left to right. You are given a s ...
- 公共的强制保留两位小数的js方法
强制保留两位小数的js方法 //写一个公共的强制保留两位小数的js方法 function toDecimal2 (x) { var f = parseFloat(x) if (isNaN(f)) { ...
- 为Vim 添加vimgdb支持
为Vim 添加vimgdb支持 1. 下载最新的vim74的源码包 wget ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2 2.下载vimgdb- ...
- Java实现发邮件功能---网易邮箱
目录 Java实现发邮件功能 前言 开发环境 代码 效果 结束语 Java实现发邮件功能 前言 电子邮件的应用场景非常广泛,例如新用户加入,即时发送优惠清单.通过邮件找回密码.监听后台程序,出现异常自 ...
- 一个SDL2.0程序的分析
//把图片加载到SDL_Texture SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){ ...