Search code examples
pythonexcelpandasif-statementvalueerror

New column on Pandas with an if statment


I am trying to recreate the blue side of the table where the equation for dnvgl shape on excel is (=IF('LENGTH (m)'>(3*DEPTH d (m)),"Flat Long shaped","Box/round shaped").

enter image description here

I tried to do this on pandas using this formula.

liftinput['DNVGL Shape']= ('Flat Long Shaped' if liftinput['LENGTH (m)'] > (3*liftinput['DEPTH d (m)']) else 'Box/Round Shaped')

I got this error - 'The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().'


Solution

  • What you're looking for is this;

    import numpy as np
    
    liftinput['DNVGL Shape'] = np.where(liftinput['LENGTH (m)'].gt(liftinput['DEPTH d (m)'].mul(3)), 'Flat Long Shaped', 'Box/Round Shaped')
    

    This is probably the most efficient way that you can do what you're trying to do.