Pandas Dataframe Regrouping

Solution 1:

Let's try pivotting:

(df.pivot(index='Cord', columns='Name', values='Name')
   .bfill().apply(lambda x: list(x.dropna()), axis=1)
)

Output:

Cord
0     [T1, T2]
5     [T1, T2]
50        [T1]
70        [T1]
dtype: object

Explanation: First we pivot the table so it looks similar to your picture:

df.pivot(index='Cord', columns='Name', values='Name')

which gives:

Name   T1   T2
Cord          
0      T1   T2
5     NaN   T2
50     T1  NaN
70     T1  NaN

So you can see the towers at all Cord levels, except that they are bottom-up. Now we use bfill to fill the missing intermediate levels, e.g. 5 on T1.

Finally, we want to aggregate along the rows, which means apply with axis=1. The lambda function is self-explained (I hope).


Update: for the updated data, we need to bfill from the first non-nan values:

(df.pivot(index='Cord', columns='Name', values='Name')
   .apply(lambda x: x[x.notna().cumsum().ne(0)].bfill())
   .apply(lambda x: list(x.dropna()), axis=1)
)

Output:

Cord
0         [T1]
5     [T1, T2]
10    [T1, T2]
50        [T1]
70        [T1]
dtype: object