I have a dataframe:
col1 col2 col3 col4 1 1 20 AA NaN 2 2 10 BB NaN 3 3 20 CC NaN 4 1 30 AA NaN 5 2 10 BB NaN I need to update col4 such that:
if col3 == 'AA': return col2 + col1 elif col2 == 'BB': return col2 - col1 else: return So the output will look something like this:
col1 col2 col3 col4 1 1 20 AA 21 2 2 10 BB 8 3 3 20 CC NaN 4 1 30 AA 31 5 2 10 BB 8 I've tried:
df['col4'][df['col3']=='AA'] = df['col2'] + df['col1'] df['col4'][df['col3']=='AA'] = df['col2'] + df['col1'] And naturally I get a SettingWithCopyWarning
What is the proper way to achieve this without the warning? I've tried apply and set_value, but I can't seem to pass so many df references into the params.