从列值中保留子字符串

2021-09-27 05:39:06 发布

您现在位置:Python中文网/ 问答频道 /正文

有谁能给我一些线索,如何将下面的数据帧转换成所需的数据帧(如下所示):

输入数据帧:

rawdata= {'id': ['json', 'molly', 'tina', 'jake', 'molly'], 'entity': ['present:k:0:mc,present:m:10:mc', 'absent:m:1:pc', 'absent:k:60:pc,absent:k:5:pc', None, 'present:k:5:mc'], 'entity2': ['present:l:300:mc', 'present:k:5:pc,present:m:0:pc', None, 'absent:l:0:pc,absent:k:10:pc', 'absent:m:60:pc']}
df= pd.DataFrame(rawdata)
df.set_index('id')

                               entity                        entity2
id                                                                  
json   present:k:0:mc,present:m:10:mc               present:l:300:mc
molly                   absent:m:1:pc  present:k:5:pc,present:m:0:pc
tina     absent:k:60:pc,absent:k:5:pc                           None
jake                             None   absent:l:0:pc,absent:k:10:pc
molly                  present:k:5:mc                 absent:m:60:pc

所需数据帧:

            entity           entity2
id                                                                  
json         0,10              300
molly         1               5,10
tina         60,5             None
jake         None             0,10
molly         5                60