1

Say I have a dataframe of weight and price.

import pandas as pd

df1 = pd.DataFrame({'weight': [1,2,0,3,4,5,0], 'price': [10,20,30,40,50,60,70]})

Question: How can I change the price column value if I spot a weight of 0? (In this case I want to set the price to 0.96 if wieght = 0 with everything else left the same). Would appreciate multiple methods along with an explanation of which to use in which scenario if possible.

Tried:

if df1['weight'] == 0:
  df1['price'] = 0.96

However when this is tried an error comes up with an error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). I know I must be doing something trivally wrong but if someone could explain this and provide the best possible solutions that would be great.

Desired Output

df1 = pd.DataFrame({'weight': [1,2,0,3,4,5,0], 'price': [10,20,0.96,40,50,60,0.96]})

Same dataframe but with the prices changed.

Curious
  • 325
  • 1
  • 10

0 Answers0