bigframes.pandas.DataFrame.where#
- DataFrame.where(cond,other=None)[source]#
Replace values where the condition is False.
Examples:
>>>df=bpd.DataFrame({'a':[20,10,0],'b':[0,10,20]})>>>df a b0 20 01 10 102 0 20[3 rows x 2 columns]
You can filter the values in the dataframe based on a condition. Thevalues matching the condition would be kept, and not matching would bereplaced. The default replacement value is
NA. For example, when thecondition is a dataframe:>>>df.where(df>0) a b0 20 <NA>1 10 102 <NA> 20[3 rows x 2 columns]
You can specify a custom replacement value for non-matching values.
>>>df.where(df>0,-1) a b0 20 -11 10 102 -1 20[3 rows x 2 columns]
Besides dataframe, the condition can be a series too. For example:
>>>df.where(df['a']>10,-1) a b0 20 01 -1 -12 -1 -1[3 rows x 2 columns]
As for the replacement, it can be a dataframe too. For example:
>>>df.where(df>10,-df) a b0 20 01 -10 -102 0 20[3 rows x 2 columns]
>>>df.where(df['a']>10,-df) a b0 20 01 -10 -102 0 -20[3 rows x 2 columns]
Please note, replacement doesn’t support Series for now. In pandas, whenspecifying a Series as replacement, the axis value should be specifiedat the same time, which is not supported in bigframes DataFrame.
- Parameters:
cond (bool Series/DataFrame,array-like, orcallable) – Where cond is True, keep the original value. Where False, replacewith corresponding value from other. If cond is callable, it iscomputed on the Series/DataFrame and returns booleanSeries/DataFrame or array. The callable must not change inputSeries/DataFrame (though pandas doesn’t check it).
other (scalar,DataFrame, orcallable) – Entries where cond is False are replaced with corresponding valuefrom other. If other is callable, it is computed on theDataFrame and returns scalar or DataFrame. The callable must notchange input DataFrame (though pandas doesn’t check it). If notspecified, entries will be filled with the corresponding NULLvalue (np.nan for numpy dtypes, pd.NA for extension dtypes).
- Returns:
DataFrame after the replacement.
- Return type: