title: Calculate unit value based multiple conditions and columns in Python
body: <p>Given a dataframe as follows:</p>
  
  <pre><code>   id    area  total_price unit
  0   1  185.00        14840    m
  1   2  215.00        15050    m
  2   3  233.23        46799    d
  3   4  122.00        17000    d
  4   5  540.00        70000    d
  5   6  415.00        78000    d
  6   7  170.00        12270    m
  7   8  410.00        30750    m
  8   9  196.00        13787    m
  9  10   55.00         3100    m
  </code></pre>
  
  <p>I would like to create a new column <code>unit_price</code> with numerical value based on the following conditions:</p>
  
  <p>a. if <code>unit</code> is <code>m</code>, then <code>unit_price</code> is calculated by <code>total_price/area/30</code>;</p>
  
  <p>b. if <code>unit</code> is <code>d</code>, then <code>unit_price</code> is calculated by <code>total_price/area</code></p>
  
  <p>This code works:</p>
  
  <pre><code>m = (df['unit'] == 'm')
  df['unit_price'] = np.where(m, df['total_price']/df['area']/30, df['total_price']/df['area'])
  </code></pre>
  
  <p>I have used also the code below but it raises an error: <code>ValueError: Wrong number of items passed 256, placement implies 1</code></p>
  
  <pre><code>def unit_price(x):
      if x['unit'] == 'm':
          return x['total_price']/x['area']/30
      if x['unit'] == 'd':
          return x['total_price']/x['area']
  
  df['unit_price'] = df.apply(unit_price, axis = 1)
  </code></pre>
  
  <p>Anyone know why I get this error, and how to fix it? Thanks.</p>