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

前端 未结 4 2056
野性不改
野性不改 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 05:01

    Linking to this question, try the following code for your dataframe:

    We can first split the column, expand it, stack it and then join it back to the original df like below:

    df.drop('category', axis=1).join(
      df['category'].str.split(',', expand=True).stack().reset_index(level=1,drop=True).rename('category'))
    

提交回复
热议问题