首页 > 其他 > 详细

一列分成多行的情况

时间:2019-09-05 10:07:08      阅读:54      评论:0      收藏:0      [点我收藏+]
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_idpred_classpredid_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_idpred_classpredid_partanimalpred1
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
 
 

一列分成多行的情况

原文:https://www.cnblogs.com/liyun1/p/11462840.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!