我有一个数据帧df
:
Index Client_ID Date
1 johndoe 2019-01-15
2 johndoe 2015-11-25
3 pauldoe 2015-05-26
我还有另一个dataframedf_prod
,产品如下:
Index Product-Type Product-Date Buyer Price
1 A 2020-01-01 pauldoe 300
2 A 2018-01-01 pauldoe 200
3 A 2019-01-01 johndoe 600
4 A 2017-01-01 johndoe 800
5 A 2020-11-05 johndoe 100
6 B 2014-12-12 johndoe 200
7 B 2016-11-15 johndoe 300
我想在df
中添加一列,该列将对当前日期已知的每种类型的最后一种产品的价格进行汇总(带Product-Date <= df.Date
)。举例说明是最好的解释方式:
对于df
的第一行
1 johndoe 2019-01-01
目前已知的johndoe
购买的最后一种A产品是:
3 A 2019-01-01 johndoe 600
(因为第四个比较老,第五个有一个Product-Date
>;Date
)
目前已知的johndoe
购买的最后一种B产品是:
7 B 2016-11-15 johndoe 300
因此df
中的行在转换后看起来像(900
是600 + 300
,两种感兴趣产品的价格):
1 johndoe 2019-01-15 900
转换后的完整df
将是:
Index Client_ID Date LastProdSum
1 johndoe 2019-15-01 900
2 johndoe 2015-11-25 200
3 pauldoe 2015-05-26 0
如您所见,有多种可能性:
df.Date
没有已知的产品(见新df
的第3行,2015年,我们不知道pauldoe
购买了任何产品)df.Date
只知道一种产品,价值是该产品的价值(见新df
的第3行,2015年,我们在johndoe
只知道一种产品,这是2014年购买的B产品,价格为200
)我找到了这个问题的解决方案,但由于我的数据帧太大,使用起来太费时了
为此,我在df
行上使用iterrows进行迭代,然后选择链接到买家的产品,在df_prod
上使用Product-Date < Date
,然后通过Product-Type
获得较旧的分组,并获得最大日期,最后我将所有产品的价格相加。
事实上,我解决了在每一行上迭代的问题(使用for iterrows),为每一行df
提取一部分df_prod
,最终得到我的和,这使得它非常长。
我几乎肯定有更好的方法来解决这个问题,比如使用pandas函数(pivot
),但我找不到方法。我已经找了很多了
提前谢谢你的帮助
非常感谢你的回答。它看起来真的很好,我接受了,因为你花了很多时间在它上面。
执行时间仍然很长,因为我没有指定一些内容。
事实上,Product-Types
不是通过买家共享的:每个买家都有自己的多种产品类型。看到这一点的真正方式如下:
Index Product-Type Product-Date Buyer Price
1 pauldoe-ID1 2020-01-01 pauldoe 300
2 pauldoe-ID1 2018-01-01 pauldoe 200
3 johndoe-ID2 2019-01-01 johndoe 600
4 johndoe-ID2 2017-01-01 johndoe 800
5 johndoe-ID2 2020-11-05 johndoe 100
6 johndoe-ID3 2014-12-12 johndoe 200
7 johndoe-ID3 2016-11-15 johndoe 300
正如你可以理解的,产品类型不是通过不同的买主分享的(事实上,它可能发生,但在我们很少考虑的情况下)问题仍然是一样的,因为您想要求和价格,所以将添加上次出现的johndoe-ID2和johndoe-ID3的价格,以获得相同的最终结果行
1 johndoe 2019-15-01 900
但正如您现在所了解的,实际上Product-Types
比Buyers
多,因此从您的答案中“获取独特的产品类型”这一步骤在初始问题上看起来非常快,实际上需要花费大量的时间
很抱歉在这一点上不清楚,我没有想到根据产品类型创建新df的可能性
主要思想是使用merge_asof获取每个产品类型和客户ID的最后日期,因此执行以下操作:
输出
问题是merge\u asof无法处理重复的值,因此我们需要创建唯一的值。这些新值是客户ID和产品类型的笛卡尔乘积,这部分内容在以下方面完成:
最后,做一个groupby并对价格求和,而不是在做填充以填充缺少的值之前
更新
你可以试试:
这里的想法是改变生成唯一值的方式
相关问题 更多 >
编程相关推荐