0

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().'

  • 1
    are you looking for `liftinput['DNVGL Shape']= liftinput.apply(lambda x: 'Flat Long Shaped' if x['LENGTH (m)'] > (3*x['DEPTH d (m)']) else 'Box/Round Shaped', axis=1)` – Epsi95 Mar 06 '21 at 15:46
  • Please avoid using `apply(...)` https://stackoverflow.com/questions/54432583/when-should-i-not-want-to-use-pandas-apply-in-my-code/54432584#54432584 – nizarcan Mar 06 '21 at 15:48

1 Answers1

1

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.

nizarcan
  • 505
  • 3
  • 15
  • This works. But can we do it on pandas without getting the value error? – Rajaa Monnapillai Mar 06 '21 at 17:06
  • You get the value error because you try to return a single value within the parantheses with multiple boolean values. (`liftinput['LENGTH (m)'] > (3*liftinput['DEPTH d (m)'])` returns lots of `False`s and `True`s in a `pandas.Series` object.) What you can do instead is something like `liftinput['DNVGL Shape'] = ['Flat Long Shaped' if liftinput['LENGTH (m)'][idx] > (3*liftinput['DEPTH d (m)'][idx]) else 'Box/Round Shaped' for idx in liftinput.index]`. But again, that is not the way to go with in a large data set. – nizarcan Mar 06 '21 at 17:37