从具有级别的数据中创建已完成的层次结构

Make fulfilled hierarchy from data with levels

我有一个 table 看起来像这样:

import pandas as pd

x = '1233312332344344'
y = ['Name_' + str(i) for i, _ in enumerate(x)]

df = pd.DataFrame({'level': list(x), 'names': y})

看起来像这样:

   level    names
0      1   Name_0
1      2   Name_1
2      3   Name_2
3      3   Name_3
4      3   Name_4
5      1   Name_5
6      2   Name_6
7      3   Name_7
8      3   Name_8
9      2   Name_9
10     3  Name_10
11     4  Name_11
12     4  Name_12
13     3  Name_13
14     4  Name_14
15     4  Name_15   

有没有办法用 pandas 重塑此 df 以获得此结果(基于级别列)?

  1       2        3      4
Name_0          
Name_0  Name_1      
Name_0  Name_1  Name_2  
Name_0  Name_1  Name_3  
Name_0  Name_1  Name_4  
Name_5          
Name_5  Name_6      
Name_5  Name_6  Name_7  
Name_5  Name_6  Name_8  
Name_5  Name_9      
Name_5  Name_9  Name_10 
Name_5  Name_9  Name_10 Name_11
Name_5  Name_9  Name_10 Name_12
Name_5  Name_9  Name_13 
Name_5  Name_9  Name_13 Name_14
Name_5  Name_9  Name_13 Name_15

这是一种源 df,但在行中填充了所有层级。

dummies = pd.get_dummies(df.level)
d = dummies.mask(dummies == 0).bfill(1).eq(1)
df.set_index('level', append=True).names.unstack().ffill().where(d, '')

level       1       2        3        4
0      Name_0                          
1      Name_0  Name_1                  
2      Name_0  Name_1   Name_2         
3      Name_0  Name_1   Name_3         
4      Name_0  Name_1   Name_4         
5      Name_5                          
6      Name_5  Name_6                  
7      Name_5  Name_6   Name_7         
8      Name_5  Name_6   Name_8         
9      Name_5  Name_9                  
10     Name_5  Name_9  Name_10         
11     Name_5  Name_9  Name_10  Name_11
12     Name_5  Name_9  Name_10  Name_12
13     Name_5  Name_9  Name_13         
14     Name_5  Name_9  Name_13  Name_14
15     Name_5  Name_9  Name_13  Name_15