我有这样一个数据帧:
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
现在,我想创建另一个数据帧(或者重写现有的数据帧),其中包含列current
和history
,以及现有的数据帧,如下所示:
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}}}
给出上下文并解释问题:run\ seq可以被视为date,如果run seq=1,则是第一天,因此leid=101可能没有历史记录,因此字典为空。
当前条目是指特定run_seq
上的条目。你知道吗
例如:如果leid 101在run seq 1上执行两个事务,那么如果在同一run seq上有两个不同的cp id对应于相同的leid,则当前将是{201:{A:{YEN:345}}, 202:{B:{USD:INR}}}
。如果两个特定leid和run seq的cp\u id相同,但购买不同的产品,则为{201:{A:{YEN:345},B:{USD:828}}}
;如果相同cp_id
,则在相同run_seq
上相同product
,然后相同{201:{A:{YEN:345, USD:734}}}
;如果特定leid和run seq的cp\u id相同cp_id
,product
,currency
,则添加amnt
,即{201:{A:{YEN:345, YEN:734}}}
,结果将为{201:{A:{YEN:1079}}}
一个特定的leid
在一个给定的run_seq would
的字典是所有前run_seq
的所有可能字典的组合。例如:如果run\ seq=5,则历史记录将是run\ seq=1、2、3、4的所有嵌套dict的组合,用于run\ seq上的特定leid。你知道吗
请注意,在输出中的特定run\ seq上应该只有一个惟一的leid。你知道吗
我什么都试过了,但没能想出一个完整的代码。更重要的是,我不知道从哪里开始?你知道吗
这是我的解决方案,但是“历史”包含了dict列表,而不是只包含dict。你知道吗
编辑:下一次尝试
我开发了Pandas的apply功能和定制Pandas的groupby功能
(定制熊猫团购积分:https://medium.com/@sean.turner026/applying-custom-functions-to-groupby-objects-in-pandas-61af58955569)
我还修改了你的输入,以显示一些可能的结果。你知道吗
代码如下所示
上述功能将导致
相关问题 更多 >
编程相关推荐