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

根据条件合并行熊猫数据框

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

根据条件合并行熊猫数据框

完全有可能:

df.groupby(((df.Start  - df.End.shift(1)) > 10).cumsum()).agg({'Start':min, 'End':max, 'Value1':sum, 'Value2': sum})

说明:

start_end_differences = df.Start  - df.End.shift(1) #shift moves the series downthreshold_selector = start_end_differences > 10 # will give you a boolean array where true indicates a point where the difference more than 10.groups = threshold_selector.cumsum() # sums up the trues (1) and will create an integer series starting from 0df.groupby(groups).agg({'Start':min}) # the aggregation is self explaining

这是与其他专栏无关的通用解决方案:

cols = df.columns.difference(['Start', 'End'])grps = df.Start.sub(df.End.shift()).gt(10).cumsum()gpby = df.groupby(grps)gpby.agg(dict(Start='min', End='max')).join(gpby[cols].sum())   Start  End  Value1  Value20      1   42      10      501    100  162      36      22


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

版权所有 (c)2021-2022 MSHXW.COM

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