栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 面试经验 > 面试问答

识别值的连续出现

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

识别值的连续出现

你可以:

df['consecutive'] = df.Count.groupby((df.Count != df.Count.shift()).cumsum()).transform('size') * df.Count

要得到:

   Count  consecutive0      1 11      0 02      1 23      1 24      0 05      0 06      1 37      1 38      1 39      0 0

在这里,您可以设置任何阈值:

threshold = 2df['consecutive'] = (df.consecutive > threshold).astype(int)

要得到:

   Count  consecutive0      1 01      0 02      1 13      1 14      0 05      0 06      1 17      1 18      1 19      0 0

或者,只需一步即可:

(df.Count.groupby((df.Count != df.Count.shift()).cumsum()).transform('size') * df.Count >= threshold).astype(int)

在效率方面,

pandas
当问题的规模变大时,使用方法可以显着提高速度:

 df = pd.concat([df for _ in range(1000)])%timeit (df.Count.groupby((df.Count != df.Count.shift()).cumsum()).transform('size') * df.Count >= threshold).astype(int)1000 loops, best of 3: 1.47 ms per loop

相比:

%%timeitl = []for k, g in groupby(df.Count):    size = sum(1 for _ in g)    if k == 1 and size >= 2:        l = l + [1]*size    else:        l = l + [0]*size    pd.Series(l)10 loops, best of 3: 76.7 ms per loop


转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/640252.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号