pandas 1.4.2

NotesParametersReturnsBackRef
where(self, cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=<no_default>)

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 <indexing.where_mask> .

Parameters

cond : bool Series/DataFrame, array-like, or callable

Where :None:None:`cond` is True, keep the original value. Where False, replace with corresponding value from other . If :None:None:`cond` is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Series/DataFrame (though pandas doesn't check it).

other : scalar, Series/DataFrame, or callable

Entries where :None:None:`cond` is False are replaced with corresponding value from other . If other is callable, it is computed on the Series/DataFrame and should return scalar or Series/DataFrame. The callable must not change input Series/DataFrame (though pandas doesn't check it).

inplace : bool, default False

Whether to perform the operation in place on the data.

axis : int, default None

Alignment axis if needed.

level : int, default None

Alignment level if needed.

errors : str, {'raise', 'ignore'}, default 'raise'

Note that currently this parameter won't affect the results and will always coerce to a suitable dtype.

try_cast : bool, default None

Try to cast the result back to the input type (if possible).

deprecated

Manually cast back if necessary.

Returns

Same type as caller or None if ``inplace=True``.

Replace values where the condition is False.

See Also

DataFrame.mask

Return an object of same shape as self.

Examples

This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> s.mask(s > 0)
0    0.0
1    NaN
2    NaN
3    NaN
4    NaN
dtype: float64
This example is valid syntax, but we were not able to check execution
>>> s.where(s > 1, 10)
0    10
1    10
2    2
3    3
4    4
dtype: int64
This example is valid syntax, but we were not able to check execution
>>> s.mask(s > 1, 10)
0     0
1     1
2    10
3    10
4    10
dtype: int64
This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> 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
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.generic.NDFrame._where pandas.core.generic.NDFrame.asof

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#9158
type: <class 'function'>
Commit: