How to create a new row for each comma separated value in a column in pandas

前端 未结 4 2061
野性不改
野性不改 2021-01-15 04:25

I have a dataframe like this:

text                   category 
sfsd sgvv              abc,xyz
zydf sefs sdfsd        yyy
dfsd dsrgd dggr        xyz
eter vxg          


        
4条回答
  •  太阳男子
    2021-01-15 04:57

    Below will give the output you need. Assuming df is your dataset name.

    new_df_skel = dict()
    new_df_skel['text'] = list()
    new_df_skel['category'] = list()
    
    for index,item in df.iterrows():
      item = dict(item)
      unref_cat = item['category']
      if "," in unref_cat:
        for strip in unref_cat.split(','):
          new_df_skel['category'].append(strip)
          new_df_skel['text'].append(item['text'])
      else:
        new_df_skel['category'].append(strip)
        new_df_skel['text'].append(unref_cat)
    
    new_dataset = pd.DataFrame(new_df_skel)
    

    Have a great day!

提交回复
热议问题