Trying to find a suitable pandas code to do this in optimized way (need to deal with millions of rows)
The input dataframe look like
levels value
0 5.1 X
1 5.1.1 Y
2 5.1.1.1 Y1
3 5.1.2 Z
4 5.2 A
5 5.2.2 B
6 5.2.2.1 C
7 5.2.2.2 D
and the output dataframe should be like
levels value newValue
0 5.1 X X,Y,Y1,Z
1 5.1.1 Y Y,Y1
2 5.1.1.1 Y1 Y1
3 5.1.2 Z Z
4 5.2 A A,B,C,D
5 5.2.2 B B,C,D
6 5.2.2.1 C C
7 5.2.2.2 D D
Rules for appending values is, when you see a level, append its value with all sub levels matching '^{level}'. For example, if it is level 5.2.2, then append all rows matching r'^5.2.2*'. If the level is 5.2. then append all rows matching r'^5.2*' Any advise?