So I want to conditionally replace values in pandas.DataFrame using mask function, so imagine we have this dataframe:
Col1 Col2
0 Qeq 10
1 Qeq 20
2 Qwa 30
What I want is to have an ability to replace all values where Col1 == 'Qwa' to f-string referencing OLD value dynamically. So my desired format is: "-{value}" and desired output is:
Col1 Col2
0 Qeq 10
1 Qeq 20
2 Qwa -30
Is it possible using .mask function?
I've tried following:
# This works but new value in this case is hardcoded
df['Col2'].mask(df['Col1'] == 'Qwa', other='-' + df['Col2'])
# This does not work as it evaluates whole series in a f-string
df['Col2'].mask(df['Col1'] == 'Qwa', other=f'-{df["Col2"]}')
dynamically? If you mean Excel style where you changeCol1,Col2automatically changes, then no, it's not supported.NEW-VALUE(OLDVALUE:{value})or any string like this