title: Derive a feature or column based on the given condition in pandas
body: <p>I have df as shown below</p>
  <pre><code>ID     Age_days    N_30     N_31_90     N_91_180      N_180_365
  1      201         60       15          30            1
  2      800         0        15          5             10
  3      800         0        0           10            6
  4      100         0        0           0             370
  5      600         0        6           5             10
  6      800         0        0           15            6
  7      500         10       10          30            9
  8      200         0        0           0             0
  9      500         0        0           0             0
  </code></pre>
  <p>From the above <code>df</code> I would like to derive a column called <code>Recency</code></p>
  <p>Explanation:</p>
  <pre><code>if df['N_30'] != 0, then Recency = (30/df['N_30'])
  elif df['N_31_90'] != 0 then Recency = 30 + (60/df['N_31_90'])
  elif df['N_91_180'] != 0 then Recency = 90 + (90/df['N_91_180'])
  elif df['N_181_365'] != 0 then Recency = 180 + (185/df['N_181_365'])
  else 
    if df['age_days'] &lt;= 365, Recency = df['age_days']
    else Recency = 366
  </code></pre>
  <p>Expected output:</p>
  <pre><code>ID     Limit    N_30     N_31_90     N_91_180      N_180_365    Recency
  1      201      60       15          30            1            (30/60) = 0.5
  2      800      0        15          5             10           30+(60/15) = 34
  3      800      0        0           10            6            90+90/10 = 100
  4      100      0        0           0             370          180+(185/370) = 180.5           
  5      600      0        6           5             10           30+(60/6) = 36
  6      800      0        0           15            6            90+(90/15) = 96
  7      500      10       10          30            9            30/10 = 3
  8      200      0        0           0             0            200
  9      500      0        0           0             0            366
  </code></pre>
  <p>I tried below code</p>
  <pre><code>pd.set_option(&quot;use_inf_as_na&quot;, True)
  df2 = df[['N_30', 'N_31_90', 'N_91_180', 'N_180_365']]
  df[&quot;Recency&quot;] = (df2.eq(0) * [30, 60, 90, 180]).sum(1) + ([30, 60, 90, 185] / df2).bfill(1).iloc[:, 0]
  df[&quot;Recency&quot;].fillna(366)
  </code></pre>