Overlapping group lines

I have a dataframe where the column leftis the leftmost place for the object and the column rightis the most suitable place. I need to group objects if they overlap, or they overlap objects that overlap (recursively). So, for example, if this is my data frame:

     left  right
0      0    4
1      5    8
2      10   13
3      3    7
4      12   19      
5      18   23
6      31   35

so the lines 0and 3overlap, so they must be in the same group, as well as the line 1is overlapping line 3- so she joins the group.

So, for this example, the output should be something like this:

     left  right    group
0      0    4         0
1      5    8         0
2      10   13        1
3      3    7         0
4      12   19        1
5      18   23        1
6      31   35        2

I thought of different directions, but did not understand (without ugly for). Any help would be appreciated!

+4
2

Wen sort_values, , shift + cumsum -

v = df.sort_values(['left', 'right'])
df.assign(Group=(v.right - v.left.shift(-1)).shift().lt(0).cumsum())

   left  right  Group
0     0      4      0
1     5      8      0
2    10     13      1
3     3      7      0
4    12     19      1
5    18     23      1
6    31     35      2
+3

, rolling max rolling min, :

df=df.sort_values(['left','right'])
df['Group']=((df.right.rolling(window=2,min_periods=1).min()-df.left.rolling(window=2,min_periods=1).max())<0).cumsum()


df.sort_index()
Out[331]: 
   left  right  Group
0     0      4      0
1     5      8      0
2    10     13      1
3     3      7      0
4    12     19      1
5    18     23      1
6    31     35      2

, (1,3) (2,4)

(3,4) - (1,2) = 1; 1 , 0;

+4

Source: https://habr.com/ru/post/1692143/


All Articles