Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.where#

DataFrame.where(cond,other=nan,*,inplace=False,axis=None,level=None)[source]#

Replace values where the condition is False.

Parameters:
condbool Series/DataFrame, array-like, or callable

Wherecond is True, keep the original value. WhereFalse, replace with corresponding value fromother.Ifcond is callable, it is computed on the Series/DataFrame andshould return boolean Series/DataFrame or array. The callable mustnot change input Series/DataFrame (though pandas doesn’t check it).

otherscalar, Series/DataFrame, or callable

Entries wherecond is False are replaced withcorresponding value fromother.If other is callable, it is computed on the Series/DataFrame andshould return scalar or Series/DataFrame. The callable must notchange input Series/DataFrame (though pandas doesn’t check it).If not specified, entries will be filled with the correspondingNULL value (np.nan for numpy dtypes,pd.NA for extensiondtypes).

inplacebool, default False

Whether to perform the operation in place on the data.

axisint, default None

Alignment axis if needed. ForSeries this parameter isunused and defaults to 0.

levelint, default None

Alignment level if needed.

Returns:
Same type as caller or None ifinplace=True.

See also

DataFrame.mask()

Return an object of same shape as self.

Notes

The where method is an application of the if-then idiom. For eachelement in the calling DataFrame, ifcond isTrue theelement is used; otherwise the corresponding element from the DataFrameother is used. If the axis ofother does not align with axis ofcond Series/DataFrame, the misaligned index positions will be filled withFalse.

The signature forDataFrame.where() differs fromnumpy.where(). Roughlydf1.where(m,df2) is equivalent tonp.where(m,df1,df2).

For further details and examples see thewhere documentation inindexing.

The dtype of the object takes precedence. The fill value is casted tothe object’s dtype, if this can be done losslessly.

Examples

>>>s=pd.Series(range(5))>>>s.where(s>0)0    NaN1    1.02    2.03    3.04    4.0dtype: float64>>>s.mask(s>0)0    0.01    NaN2    NaN3    NaN4    NaNdtype: float64
>>>s=pd.Series(range(5))>>>t=pd.Series([True,False])>>>s.where(t,99)0     01    992    993    994    99dtype: int64>>>s.mask(t,99)0    991     12    993    994    99dtype: int64
>>>s.where(s>1,10)0    101    102    23    34    4dtype: int64>>>s.mask(s>1,10)0     01     12    103    104    10dtype: int64
>>>df=pd.DataFrame(np.arange(10).reshape(-1,2),columns=['A','B'])>>>df   A  B0  0  11  2  32  4  53  6  74  8  9>>>m=df%3==0>>>df.where(m,-df)   A  B0  0 -11 -2  32 -4 -53  6 -74 -8  9>>>df.where(m,-df)==np.where(m,df,-df)      A     B0  True  True1  True  True2  True  True3  True  True4  True  True>>>df.where(m,-df)==df.mask(~m,-df)      A     B0  True  True1  True  True2  True  True3  True  True4  True  True

[8]ページ先頭

©2009-2025 Movatter.jp