C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas DataFrame.where()The main task of the where() method is to check the data frame for one or more conditions and return the result accordingly. By default, if the rows are not satisfying the condition, it is filled with NaN value. SyntaxDataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False, raise_on_error=None) Parameters
ReturnsExample1import pandas as pd import numpy as np a = pd.Series(range(5)) a.where(a > 0) a.mask(a > 0) a.where(a > 1, 10) info = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B']) info b = info % 3 == 0 info.where(b, -info) info.where(b, -info) == np.where(b, info, -info) info.where(b, -info) == info.mask(~b, -info) Output A B 0 True True 1 True True 2 True True 3 True True 4 True True
Next TopicAdd column to DataFrame columns
|