I need to replace a NULL
value in a column only when other conditions are matched.
Columns: Parent, Child, flag01, lag02
Parent columns has many NULL
values, but I want to replace the null
values only when flag01
and flag02
is "ok".
If flag01
and flag02
are both "Ok
" and Parent is NULL, replace to 'CT_00000'. Else, keep the original value (when NOT NULL).
To replace a values in a column based on a condition, using DataFrame.loc, use the following syntax. DataFrame.loc[condition, column_name] = new_value In the following program, we will replace those values in the column ‘a’ that satisfy the condition that the value is less than zero.
The solutions that can be used to change the DataFrame column values based on some condition are as below: There are times when we need to change the values of specific columns in our DataFrame, based on certain conditions. For instance, we might want to set a value in a column to 1 if the value in another column is greater than 6.
The values in column 'C' are all initialized to 0. The code then uses the mask () method to update the values in column ' C '. The mask () method takes three arguments. The first argument is a condition - in this case, the condition is df ['B'] > 6. The second argument is the value to use if the condition is True - in this case, the value is 1.
Syntax: df.loc [ df [“column_name”] == “some_value”, “column_name”] = “value” value = The value that should be placed instead. Note: You can also use other operators to construct the condition to change numerical values.. Another method we are going to see is with the NumPy library.
So as I though you want a select statement.
select case when (parent is null and flag01 = 'OK' and flag02 = 'OK')
then 'CT_00000'
else parent end as columnSomeName,
Child, flag01, lag02
from yourTable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With