I have a Pandas DataFrame:
import pandas as pd
df = pd.DataFrame({'col1': ['a','a','b','b'],
'col2': [1,2,3,4],
'col3': [11,12,13,14]})
col1 col2 col3
0 a 1 11
1 a 2 12
2 b 3 13
3 b 4 14
I need to replace an entry in col2 by some function of the row's col2 and col3 values if the value in col1 is a b, but leave the rows unchanged if the value in col1 is not a b. Say the function is col3 * exp(col2), then applying this to df above would yield
col1 col2 col3
0 a 1 11
1 a 2 12
2 b 261.1 13
3 b 764.4 14
Ideally this would be vectorised and in-place as my real DataFrame has a few million rows.
This differs from other questions on Stack Overflow as they only require either the new value to not depend on other columns or to change all rows at once. Thank you.
Edit: Corrected the target DataFrame. Had changed the function from exp(col2)+col3 to exp(col2)*col3 without updating the values in the example.