熊猫:在时间间隔熊猫中出现的滴剂
我们有一个包含“ ID”和“ Day”列的数据框架,该列表显示了何时提出投诉。我们需要从“ ID”列中删除重复项,但前提是相距30天,只有重复的副本。请参阅下面的示例:
当前数据集:
ID DAY
0 1 22.03.2020
1 1 18.04.2020
2 2 10.05.2020
3 2 13.01.2020
4 3 30.03.2020
5 3 31.03.2020
6 3 24.02.2021
目标:
ID DAY
0 1 22.03.2020
1 2 10.05.2020
2 2 13.01.2020
3 3 30.03.2020
4 3 24.02.2021
是否有建议?我尝试了Groupby,然后创建一个循环来计算每种组合之间的差异,但是由于数据框有数百万的行,这将永远花费...
We have a dataframe containing an 'ID' and 'DAY' columns, which shows when a specific customer made a complaint. We need to drop duplicates from the 'ID' column, but only if the duplicates happened 30 days apart, tops. Please see the example below:
Current Dataset:
ID DAY
0 1 22.03.2020
1 1 18.04.2020
2 2 10.05.2020
3 2 13.01.2020
4 3 30.03.2020
5 3 31.03.2020
6 3 24.02.2021
Goal:
ID DAY
0 1 22.03.2020
1 2 10.05.2020
2 2 13.01.2020
3 3 30.03.2020
4 3 24.02.2021
Any suggestions? I have tried groupby and then creating a loop to calculate the difference between each combination, but because the dataframe has millions of rows this would take forever...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以计算每组连续日期之间的差异,并使用它形成一个面具以删除相距不到30天的天数:
nb。这种方法的潜在缺点是,如果客户在30天内提出了新的投诉,则可以重新启动下一个投诉的门槛
输出:
因此,另一种方法可能是
resplame
小组到30天:输出:
You can compute the difference between successive dates per group and use it to form a mask to remove days that are less than 30 days apart:
NB. the potential drawback of this approach is that if the customer makes a new complaint within the 30days, this restarts the threshold for the next complaint
output:
Thus another approach might be to
resample
the data per group to 30days:output:
您可以通过
ID
列和diff
day
列中的 conter 列转换为原始日期格式,您可以使用
dt .Strftime
You can try group by
ID
column anddiff
theDAY
column in each groupTo convert to original date format, you can use
dt.strftime