有一个pandas表结构如下:
lable ci grouplable
5 3 38076011 3;5;7
8 5 38076031 5
3 2 38076041 2;4
2 2 38076051 2;4;6
10 6 38076061 6
1 1 38076097 1;3;7
0 1 38076098 1
需要将grouplable列中相关性字段区分出来,并加以合并,结果如下:
grouplable fitcell
0 5 1;3;5;7
1 1;3;7 1;3;5;7
2 1 1;3;5;7
3 2;4 2;4;6
4 6 2;4;6
5 3;5;7 1;3;5;7
代码如下:
glgroup=cgroup[‘grouplable’].values.tolist()
glreault= {}
for a in glgroup:
print(a)
print(‘ppppppppppppppppp’)
# if len(b)>len(a):
tt=glgroup.copy()
tt.remove(a)
for i in range(10):
orga=a
for x in tt:
if len(set(a.split(‘;’)).intersection(set(x.split(‘;’)))) > 0 and len(set(a.split(‘;’)).difference(set(x.split(‘;’)))) > 0:
# print(a,x)
a=‘;’.join(sorted(list(set(a.split(‘;’)).union(set(x.split(‘;’))))))
glreault[x]=a
print(a)
print(orga)
print(x)
print(‘fit’)
if len(a)==len(orga):
print(‘brk’)
print(a)
print(orga)
break
# print(‘pppppppppppppppppp’)
print(glreault)
gllist=[]
for d in glreault:
gllist.append([d,glreault[d]])
glreault=pd.DataFrame(gllist,columns=[‘grouplable’,‘fitcell’])
print(glreault)
cgroup=cgroup.merge(glreault,how=‘left’,on=‘grouplable’)
print(cgroup)
cgroup.loc[cgroup.fitcell.isnull()==True,‘fitcell’]=cgroup[cgroup.fitcell.isnull()==True].apply(lambda x:‘;’.join(sorted(x.grouplable.split(‘;’))),axis=1)
print(cgroup)