一列分成多行的情况

data=pd.DataFrame({'v_id':['d1','d2'],
                   'pred_class':['cat,dog','other_label,fish'],
                   'pred':[[0.72,0.65],[0.11,0.23]],
                  'id_part':['d',5],})
 
 
In [240]:
data
 
 
Out[240]:
  v_id pred_class pred id_part
0 d1 cat,dog [0.72, 0.65] d
1 d2 other_label,fish [0.11, 0.23] 5
In [247]:
 
animal=data['pred_class'].str.split(',',expand=True).stack().reset_index(level=1,drop=True).rename('animal')
 
 
In [256]:
  data['pred_class'].values
 
 
Out[256]:
array(['cat,dog', 'other_label,fish'], dtype=object)
In [261]:
animal
 
Out[261]:
0            cat
0            dog
1    other_label
1           fish
Name: animal, dtype: object
 
 
In [272]:
pred=np.concatenate(data['pred'].values) #concatenate()函数的用法
pred1=pd.DataFrame({'pred1':pred})
 
 
In [277]:
pred1
Out[277]:
  pred1
0 0.72
1 0.65
2 0.11
3 0.23

In[278]:

data.join(animal).reset_index().join(pred1)

Out[278]:


index
v_id pred_class pred id_part animal pred1
0 0 d1 cat,dog [0.72, 0.65] d cat 0.72
1 0 d1 cat,dog [0.72, 0.65] d dog 0.65
2 1 d2 other_label,fish [0.11, 0.23] 5 other_label 0.11
3 1 d2 other_label,fish [0.11, 0.23] 5 fish 0.23
 
 

猜你喜欢

转载自www.cnblogs.com/liyun1/p/11462840.html