python - Pandas 组合连续值

标签 python pandas dataframe pandas-groupby

我正在尝试组合 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/

相关文章:

python - 如何使用python检测多个USB端口并从USB读取数据

python-3.x - Pandas:按数据帧行索引的前两个字符进行分组

python - 尝试按对象将行附加到组中的每个组时出现奇怪的行为

python - 如何在 Pandas 数据帧切片中使用 apply 来设置多列的值

python - 使用可变列对 Pandas 数据框进行排序

PYTHON3.3 >> sybpydb.so : undefined symbol: PyUnicodeUCS2_Decode

python - 使用 python 将数据复制并粘贴到 Excel 中(保留源格式)

python - 通过内省(introspection)行的每个元素来过滤 pandas 数据框

R : How to split the accumulated value to missing values in data frame?

Python,列表中第一个对象中的最小值