What I want to achieve: Condition: where column2 == 2 leave to be 2 if column1 < 30 elsif change to 3 if column1 > 90
This can be simplified into
where (column2 == 2 and column1 > 90) set column2 to 3. The
column1 < 30 part is redundant, since the value of
column2 is only going to change from 2 to 3 if
column1 > 90.
In the code that you provide, you are using pandas function replace, which operates on the entire Series, as stated in the reference:
Values of the Series are replaced with other values dynamically. This differs from updating with .loc or .iloc, which require you to specify a location to update with some value.
This means that for each iteration of
for x in filter1 your code performs global replacement, which is not what you want to do - you want to update the specific row of
column2 that corresponds to
column1 (which you are iterating over).
the problem is 2 does not change to 3 where column1 > 90
This is truly strange. I would expect the code you provided to have changed every instance of 2 in
column2 to 3 as soon as it encountered an
x >= 30, as dictated by your code conditional statement (the execution of the
else branch). This discrepancy may stem from the fact that you are assigning to
column2 the result of global replacement performed on the column
Output (the contents of which are unknown). In any case, if you want your program to do something under a specific condition, such as
x > 90, it should be explicitly stated in the code. You should also note that the statement
data['column2'] = data['column2'].replace(, ) achieves nothing, since 2 is being replaced with 2 and the same column is both the source and the destination.
What you could use to solve this particular task is a boolean mask (or the query method). Both are explained in an excellent manner in this question.
Using a boolean mask would be the easiest approach in your case:
mask = (data['column2'] == 2) & (data['column1'] > 90)
data['column2'][mask] = 3
The first line builds a Series of booleans (True/False) that indicate whether the supplied condition is satisfied.
The second line assigns the value 3 to those rows of
column2 where the mask is True.