迭代地计算熊猫组差异
嗨,我根据前后的行创建了一个名为“组”的列,是水果。
如何创建new_group列?它基于10分钟的水果差距。数据帧按时间,时间分类。
person time_bought product group new_group
abby 2:21 fruit 1 1
abby 2:24 other
abby 2:25 fruit 2 1 (2.25 is within 10 minutes of 2.21 so part of same group)
abby 10:35 fruit 2 2
abby 10:40 other
abby 10:42 fruit 3 2 (10.42 is within 10 minutes of 10.35)
abby 10:53 fruit 4 3 (10.53 is not within 10 minutes of 10.42)
abby 11:04 fruit d
barry 12:00 fruit 1
我试过
m= df.groupby(["person", "group"]).time_bought.diff()
df["new_group"] = df.groupby(["person, "group"]).mask(m).ffill()
Hi I created a column called "group" based on if the row before and after is fruit.
How could I create the new_group column? It's based on 10-minute fruit gaps. The dataframe is sorted by person, time.
person time_bought product group new_group
abby 2:21 fruit 1 1
abby 2:24 other
abby 2:25 fruit 2 1 (2.25 is within 10 minutes of 2.21 so part of same group)
abby 10:35 fruit 2 2
abby 10:40 other
abby 10:42 fruit 3 2 (10.42 is within 10 minutes of 10.35)
abby 10:53 fruit 4 3 (10.53 is not within 10 minutes of 10.42)
abby 11:04 fruit d
barry 12:00 fruit 1
I tried
m= df.groupby(["person", "group"]).time_bought.diff()
df["new_group"] = df.groupby(["person, "group"]).mask(m).ffill()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要生成新组,您可以使用:
输出:
潜在掩蔽:掩码非保证和组的最后(逻辑不清楚):
输出:
To generate your new group, you can use:
output:
Potential masking: mask non-fruit and last of group (the logic is unclear):
output: