I need to make a mathematical operation which is conditional on the value in a second column. Here is the setup.
Given a simple dataframe (df):
df
It was throwing ValueError since you were not using the loc correctly. Here is the solution using loc:
ValueError
df.loc[:,'math'] = 10 + df.loc[df['col1'] == "B", 'col3']
Output:
col1 col2 col3 math 0 A 2 0 NaN 1 A 1 1 NaN 2 B 9 9 19.0 3 NaN 8 4 NaN 4 D 7 2 NaN 5 C 4 3 NaN