关于模块pandas.core.component.generic.generic.中的函数where的帮助。
where(self, cond, other=nan, inplace=False, axis=None, level=None, errors='' raise', try_cast=False)
替换条件为False的值。
参数
cond : boolean Series/DataFrame、数组式或可调用的系列/数据框
当`cond`为True时,保留原值。其中
False,用`other`中的相应值代替。
如果`cond`是可调用的,它将在系列/数据框上计算,并在
应该返回布尔系列/数据框或数组。可调取的对象必须是
不改变输入系列/数据框(虽然pandas不检查)。
...版本添加:: 0.18.1
一个callable可以作为conc.
其他: scalar、Series/DataFrame或callable
`cond`为False的条目被替换为
`other`中的相应值。
如果other是可调用的,则在Series/DataFrame上计算,并且
应该返回标量或 Series/DataFrame。可调取的对象不能是
更改输入系列/数据框(虽然pandas不检查)。
...版本添加:: 0.18.1
一个可调用的函数可以作为其他函数使用。
inplace : bool, 默认为False
是否对数据进行原地操作。
axis : int, 默认为 None
如果需要的话,可选择对齐轴。
level : int, 默认为 None
如果需要的话,对齐级别。
errors : str, {''提高', '忽略'}, 默认为'提高'
注意,目前这个参数不会影响到
结果,并且总是会强制执行到一个合适的dtype。
- ' raise':允许提出异常。
- '忽略':压制异常。错误时,返回原始对象。
try_cast : bool, 默认为False
尝试将结果投回输入类型(如果可能的话)。
返回
-------
与呼叫者相同的类型
另见
--------
:func:`DataFrame.mask`:返回一个与 "DataFrame.mask "形状相同的对象。
自己。
笔记
-----
where方法是if-then这个成语的应用。对于每一个
元素,如果``cond`为`True``,则在调用数据框中的
元素;否则,将使用DataFrame中的相应元素。
``其他````使用了。
:func:`DataFrame.where`的签名不同于
:func:`numpy.where`。粗略来说,``df1.where(m, df2)``等于
``np.where(m, df1, df2)``。
更多的细节和例子,请参见 "where "文档中的 "where"。
:ref:`indexing <indexing.where_mask>`。
Examples
--------
>>> s = pd.Series(range(5))
>>> s.where(s > 0)
0 NaN
1 1.0
2 2.0
3 3.0
4 4.0
dtype: float64
>>> s.mask(s > 0)
0 0.0
1 NaN
2 NaN
3 NaN
4 NaN
dtype: float64
>>> s.where(s > 1, 10)
0 10
1 10
2 2
3 3
4 4
dtype: int64
>>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
>>> df
A B
0 0 1
1 2 3
2 4 5
3 6 7
4 8 9
>>> 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
pandas中where函数的用法
猜你喜欢
转载自blog.csdn.net/sinat_23971513/article/details/105276744
今日推荐
周排行