lundi 28 octobre 2019

How to replace only 0 values in a column by multiplication of 2 columns in Dataframe with a loop?

Here is my Dataframe: df={'pack':[2,2,2,2], 'a_cost':[10.5,0,11,0], 'b_cost':[0,6,0,6.5]} It should look like this

At this point you will find that a_cost and b_cost columns have 0s where other column has a value. I would like my function to follow this logic...

for i in df.a_cost: if i==0: b_cost(column):value *(multiply) pack(column):value replace 0 with this new multiplied value (example: 6.0*2=12) for i in df_b.cost: if i==0: a_cost(column):value /(divide) pack(column):value replace 0 with this new divided value (example: 10.5/2=5.25)

I can't figure out how to write this logic successfully... Here is the Dataframe I expect to see at the end; in code format: df={'pack':[2,2,2,2], 'a_cost':[10.5,12.0,11,13.0], 'b_cost':[5.25,6,5.50,6.5]}

Help is really appreciated!

Aucun commentaire:

Enregistrer un commentaire