zoukankan      html  css  js  c++  java
  • 一列分成多行的情况

    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
     
     
  • 相关阅读:
    冲刺4
    冲刺3
    冲刺2
    冲刺一
    构建之法阅读笔记04
    数组02开发日志
    进度条第七周
    《构建之法》阅读问题
    软件工程概论第一节
    《大道至简》弟七八章读后感
  • 原文地址:https://www.cnblogs.com/liyun1/p/11462840.html
Copyright © 2011-2022 走看看