Python pandas - 如何对封闭元素进行分组

标签 python algorithm pandas group-by

我有一个数据框,我需要在其中对距离不超过 1 的元素进行分组。 例如,如果这是我的 df:

     group_number  val
0              1    5
1              1    8
2              1   12
3              1   13
4              1   22
5              1   26
6              1   31
7              2    7
8              2   16
9              2   17
10             2   19
11             2   29
12             2   33
13             2   62

所以我需要按 group_numberval 进行分组,其中 val 的值小于或等于 1。

所以,在这个例子中,行 23 将组合在一起,行 89 会聚在一起。

我尝试使用 diff 或相关函数,但我没有弄明白。

任何帮助将不胜感激!

最佳答案

使用 diff 是正确的方法 - 只需将它与 gtcumsum 结合使用,您就拥有了自己的组。

想法是对大于阈值的差异使用累积和。大于阈值的差异将变为 True。相反,等于或低于您的阈值的差异将变为 False。对 bool 值的累积求和将使等于或低于您的阈值的差异保持不变,因此它们获得相同的组号。

max_distance = 1

df["group_diff"] = df.sort_values("val")\
                     .groupby("group_number")["val"]\
                     .diff()\
                     .gt(max_distance)\
                     .cumsum()

print(df)

    group_number    val group_diff
0   1               5   0
1   1               8   1
2   1               12  2
3   1               13  2
4   1               22  5
5   1               26  6
6   1               31  8
7   2               7   0
8   2               16  3
9   2               17  3
10  2               19  4
11  2               29  7
12  2               33  9
13  2               62  10

您现在可以在 group_numbergroup_diff 上使用 groupby 并查看结果组,结果如下:

grouped = df.groupby(["group_number", "group_diff"])
print(grouped.groups)

{(1, 0): Int64Index([0], dtype='int64'),
 (1, 1): Int64Index([1], dtype='int64'),
 (1, 2): Int64Index([2, 3], dtype='int64'),
 (1, 5): Int64Index([4], dtype='int64'),
 (1, 6): Int64Index([5], dtype='int64'),
 (1, 8): Int64Index([6], dtype='int64'),
 (2, 0): Int64Index([7], dtype='int64'),
 (2, 3): Int64Index([8, 9], dtype='int64'),
 (2, 4): Int64Index([10], dtype='int64'),
 (2, 7): Int64Index([11], dtype='int64'),
 (2, 9): Int64Index([12], dtype='int64'),
 (2, 10): Int64Index([13], dtype='int64')}

感谢@jezrael 提示避免使用新列以提高性能:

group_diff = df.sort_values("val")\
               .groupby("group_number")["val"]\
               .diff()\
               .gt(max_distance)\
               .cumsum()

grouped = df.groupby(["group_number", group_diff])

关于Python pandas - 如何对封闭元素进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48109624/

相关文章:

python - matplotlib 中没有绘图窗口

algorithm - 如果 IP 在范围列表中,则过滤它们

python - 在数据帧python中找到并加上不足的日期时间

python - 我无法列出 Raspberry Pi 附近的 BLE 设备(python、btmgmt)

python - 找不到 'stackless_installer_c4_win32' 的索引页(可能拼写错误?)

带有通配符的java黑名单

c - 为给定掩码生成所有位模式

python - Pandas:通过多列查找另一个DataFrame中不存在的行

python - pandas df.corr() 返回 NaN,尽管数据已填充数据

python - 在 Python 脚本 MySQL 查询中使用 RegEx 进行 IP 地址验证