title: How to create "dynamic" features for a machine learning model using pandas?
body: <p>I am preparing data for a machine learning model which makes predictions (target variable not shown below) at CID,transaction_dt level.</p>
  
  <p>I want to create a feature which give me sum of sales (I am calling this dynamic because it changes for each CID,transaction_dt) for each CID before the particular transaction_dt for each CID,transaction_dt combinations.</p>
  
  <p>Similarly I want to create a binary feature for Credit_flag which tells me if the CID used credit card anytime before the transaction date.</p>
  
  <p><strong>Input</strong></p>
  
  <pre><code>CID transaction_dt  sales   Credit_flag
  1000    01-09-15    460         1
  1000    01-31-2015  300         0
  1000    01-03-15    400         1
  1000    01-12-15    240         1
  2001    02-06-15    389         1
  2001    01-03-15    429         0
  2001    04-30-2015  491         0
  2001    01-09-15    148  `      1
  </code></pre>
  
  <p><strong>Desired Output</strong></p>
  
  <p>Note:pre_sales =sum of sales for the CID before the transaction_dt</p>
  
  <pre><code>CID transaction_dt  pre_sales   pre_Credit_flag
  1000    01-09-15    400             1
  1000    01-31-2015  1100            1
  1000    01-03-15    0               0
  1000    01-12-15    860             1
  2001    02-06-15    577             1
  2001    01-03-15    0               0
  2001    04-30-2015  966             1
  2001    01-09-15    429             0
  </code></pre>
  
  <p>How can I do this elegantly using pandas?
  I tried creating a function as shown below but its wont work.</p>
  
  <pre><code>def dynamic_var(cid, trans_dt):
      x_data1_dyna=x_data1_dedup[(x_data1_dedup['transaction_dt']&lt;trans_dt) &amp; (x_data1_dedup['cid']==cid)].groupby(['cid','transaction_dt'])\
      .agg({'sales':'sum','credit_flaf':'max'}).reset_index()
  
      return x_data1_dyna
  
  
  pd.concat(x_data1_dedup.apply(lambda x: dynamic_car(x['cid'], x['transaction_dt']),axis=1),axis=1)
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I am looking for a generic approach which works even if I have 100 columns instead of two columns given here &amp; I want to apply different aggregation logic say, take median for few columns, max for another set of columns, count for another set of columns based on the same transaction_dt logic discussed above?</p>