gpt4 book ai didi

python - Pandas 组合连续值

转载 作者:行者123 更新时间:2023-11-28 21:36:16 25 4
gpt4 key购买 nike

我正在尝试组合 pandas 中的连续值来创建新的输出数据帧,并且需要一些帮助。

输入:

   depth   type
0 50 0
1 100 1
2 150 1
3 200 1
4 250 0
5 300 0
6 350 0
7 400 1
8 450 1
9 500 0

输出:

   start_depth   stop_depth  type
0 100 200 1
1 400 450 1

最佳答案

遵循 Wen 的agg min/max建议:

c=(df.type != df.type.shift()).cumsum().values
ndf = df.groupby([c, 'type']).depth.agg(['min','max']).reset_index(level=1)

type start end
1 0 50 50
2 1 100 200
3 0 250 350
4 1 400 450
5 0 500 500

要获取type==1,只需选择

ndf[ndf.type==1]

type start end
2 1 100 200
4 1 400 450

关于python - Pandas 组合连续值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51296227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com