0

I have my data set where I have to fill the missing values in the Column "Extended Price" with a formula with other columns that is = Quantity Ordered * Unit Cost. I have tried this function it works but keep changing for me the non missing values from their original values to 0 because of else part.

How do I keep the values for the non missing values and use the function only to make changes on the missing values.

Here is my code function.

df['Extended Cost'] = df.apply(lambda var: var['Quantity Ordered'] * var['Unit Cost'] if var['Extended Cost'] != var['Extended Cost'] else 0, axis=1) 

Suggestions and help are appreciated.

Timus
  • 10,974
  • 5
  • 14
  • 28
  • Welcome to stack overflow. Please [edit] your question to include a [mcve] with a sample of your input data and expected output so that we can better understand your question. Assuming you're using pandas, see [How to make a good pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Feb 22 '22 at 23:14
  • Also, under what circumstance would `if var['Extended Cost'] != var['Extended Cost']` be true? Isn't the value always equal to itself? – G. Anderson Feb 22 '22 at 23:20
  • I want to perform the formula when var['Extended Cost'] is Null value. since this columns contains float values and other empty missing values. – Celine Ahmad Feb 22 '22 at 23:22

1 Answers1

0

Set the value equal to itself in the else case. that way you aren't changing the data if it exists.

Also your if condition should be around the other way around, i.e. do something if the value is None, else do nothing

df['Extended Cost'] = df.apply(lambda var: var['Quantity Ordered'] * var['Unit Cost'] if var['Extended Cost'] == None else var['Extended Cost'], axis=1)
QuantumMecha
  • 1,514
  • 3
  • 22