title: construct new columns based on some condition from multiple columns and rows in a dataframe
body: <p>I have a dataframe that looks like this :</p>
  
  <pre><code>leid     run_seq     cp_id     products    currency     amount
  101           1       201        A           YEN         345
  102           2       201        B           INR         223
  101           2       202        A           USD         845
  102           3       201        C           USD         345
  102           3       203        A           INR         747
  </code></pre>
  
  <p>Now I want to create another data frame (or may be rewrite the existing one) which has columns <code>current</code> and <code>history</code> along with the existing ones, that would look like :</p>
  
  <pre><code>leid     run_seq     current                                     History
  101           1       {201:{A:{YEN:345}}}                          {}
  102           2       {201:{B:{INR:223}}}                          {}
  101           2       {202:{A:{USD:845}}}                          {201:{A:{YEN:345}}}
  102           3       {201:{C:{USD:773}},203:{A:{INR:747 }         {201:{B:{INR:223}}}
  </code></pre>
  
  <p>To give context and explain the problem : run_seq can be treated as date, if run seq = 1 , its the first day and hence there could be no history for leid = 101, hence the empty dictionary.
  current entry refers to the entry on that particular <code>run_seq</code>. </p>
  
  <p>For example : If leid 101 does two transactions on run_seq 1  then the current would be <code>{201:{A:{YEN:345}}, 202:{B:{USD:INR}}}</code> if there are two different cp id's corresponding to same leid on same run_seq. If the cp_ids are same for two particular leid and run_seq but buy different products then <code>{201:{A:{YEN:345},B:{USD:828}}}</code>; if same <code>cp_id</code>,on same <code>run_seq</code> same <code>product</code> and same then <code>{201:{A:{YEN:345, USD:734}}}</code>;if same <code>cp_id</code>, <code>product</code>, <code>currency</code> for a particular leid and run_seq then add the <code>amnt</code> i.e <code>{201:{A:{YEN:345, YEN:734}}}</code>, the result would be <code>{201:{A:{YEN:1079}}}</code></p>
  
  <p>Hisotry for a particular <code>leid</code> at a given <code>run_seq would</code> be combination of all the posssible dictionaries for the all previous <code>run_seq</code>. For example : If run_seq = 5, history would be combination of all the nested dicts for run_seq = 1,2,3,4 for that particular leid on a run_seq.</p>
  
  <p>Note that there should be only one unique leid on a particular run_seq in the output.</p>
  
  <p>I have tried everything, but am not able to come up with a complete code. More to say, I cannot think where to start from ?</p>