-1

I only want to multiple if the column 'Do it?'' contains the word yes in a new column, since I need to sum everything in the end.

data = {'Do it?': ['yes', 'no', 'no', 'no','yes'],'Price': [12,15,10,2,1]}

df = pd.DataFrame(data)

Output:

    Do it?  Price
0   yes     12
1   no      15
2   no      10
3   no      2
4   yes     1

What I've tried

if df['Do it?'] == 'yes':
  df['Result'] = df['Price'] * 10
print(df['Result'].sum())
 
Error : The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

1 Answers1

1

I believe this is what you are looking for

df.loc[df['Do it?'] == 'yes', 'Price'] = df.Price * 10

Output:

  Do it?  Price
0    yes    120
1     no     15
2     no     10
3     no      2
4    yes     10
Patrick
  • 1,189
  • 5
  • 11
  • 19