Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.Series.case_when#

Series.case_when(caselist)[source]#

Replace values where the conditions are True.

Parameters:
caselistA list of tuples of conditions and expected replacements

Takes the form:(condition0,replacement0),(condition1,replacement1), … .condition should be a 1-D boolean array-like objector a callable. Ifcondition is a callable,it is computed on the Seriesand should return a boolean Series or array.The callable must not change the input Series(though pandas doesn`t check it).replacement should be a1-D array-like object, a scalar or a callable.Ifreplacement is a callable, it is computed on the Seriesand should return a scalar or Series. The callablemust not change the input Series(though pandas doesn`t check it).

Added in version 2.2.0.

Returns:
Series

See also

Series.mask

Replace values where the condition is True.

Examples

>>>c=pd.Series([6,7,8,9],name='c')>>>a=pd.Series([0,0,1,2])>>>b=pd.Series([0,3,4,5])
>>>c.case_when(caselist=[(a.gt(0),a),# condition, replacement...(b.gt(0),b)])0    61    32    13    2Name: c, dtype: int64

[8]ページ先頭

©2009-2025 Movatter.jp