I have a dataframe that looks like this :
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
Now I want to create another data frame (or may be rewrite the existing one) which has columns current and history along with the existing ones, that would look like :
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}}}
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 run_seq.
For example : If leid 101 does two transactions on run_seq 1 then the current would be {201:{A:{YEN:345}}, 202:{B:{USD:INR}}} 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 {201:{A:{YEN:345},B:{USD:828}}}; if same cp_id,on same run_seq same product and same then {201:{A:{YEN:345, USD:734}}};if same cp_id, product, currency for a particular leid and run_seq then add the amnt i.e {201:{A:{YEN:345, YEN:734}}}, the result would be {201:{A:{YEN:1079}}}
Hisotry for a particular leid at a given run_seq would be combination of all the posssible dictionaries for the all previous run_seq. 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.
Note that there should be only one unique leid on a particular run_seq in the output.
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 ?
For exampleparagraph toDataFramefor minimal, complete, and verifiable example?