mardi 27 octobre 2020

If - else function to calculate value if cell isn't empty

I need to make a calculation based on the data inside the imported Excel File. If there is a value inside Col1 the formula should take the value from Col1 and multiply it with Col4. If the value in Col1 is empty it should take the Value from Col2 and multiply it with Col4. The result of the calculation that is done, should be added into a new column.

Currently it works for the 2nd part of my calculation but the code doesn't recognize if there is a value inside Col1. It is like, it takes for all the further data just the first row as reference because there is nothing inside Col1.

This is my current code:

if merged_data['Col1'].isna().any():
    merged_data['Col4'] = (merged_data['Col2'] / merged_data['Col3'])
else:
    merged_data['Col4'] = (merged_data['Col1'] / merged_data['Col3'])

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire