janitor.update_where

janitor.update_where(df: pandas.core.frame.DataFrame, conditions: Any, target_column_name: Hashable, target_val: Any) → pandas.core.frame.DataFrame[source]

Add multiple conditions to update a column in the dataframe.

This method mutates the original DataFrame.

Example usage:

# The dataframe must be assigned to a variable first.
data = {
    "a": [1, 2, 3, 4],
    "b": [5, 6, 7, 8],
    "c": [0, 0, 0, 0]
}
df = pd.DataFrame(data)
df = (
    df
    .update_where(
        condition=("a > 2 and b < 8",
        target_column_name='c',
        target_val=10)
    )
# a b  c
# 1 5  0
# 2 6  0
# 3 7 10
# 4 8  0
Parameters
  • df – The pandas DataFrame object.

  • conditions – Conditions used to update a target column and target value.

  • target_column_name – Column to be updated. If column does not exist in dataframe, a new column will be created; note that entries that do not get set in the new column will be null.

  • target_val – Value to be updated

Returns

An updated pandas DataFrame.

Raises
  • IndexError – if conditions does not have the same length as df.

  • TypeError – if conditions is not a pandas-compatible string query.