I am trying to create a new column in pandas using an if statement. I have this df:
df = {'Col1': [7,6,-9],
'Col2': [0.5,0.5,0.5],
'Col3': [5,4,3]}
If Col1 is greater than 0, then I'd like to multiply Col2 by Col3 to create the new column, Col4. If Col1 is not greater than 0, then I'd just like to return 0 as the column value.
Here is what I tried:
df['Col4'] = if df['Col1'] > 0:
df['Col2'] * df['Col3']
else:
0
I get the error: "SyntaxError: invalid syntax"
The final answer should look like this:
df = {'Col1': [7,6,-9],
'Col2': [0.5,0.5,0.5],
'Col3': [5,4,3],
'Col4': [2.5,2,0]}
Note that because in Col1 "-9" is not greater than 0, Col4 should give 0.