Is there any possibility to separate keyword if there are more than one in one row

Viewed 49

Is there any possibility to separate keyword if there are more than one in one row. Its actually the data from a survey to know brand awareness.

For example if i have df like this:

df

ID                             Brand
1                              BMW
2                              VW-BMW-Audi
3                              Audi
4                              Mercedes-Audi

my desired output would be like this:

ID                             Brand
1                              BMW
2                              VW
2                              BMW
2                              Audi
3                              Audi
4                              Mercedes
4                              Audi

So I would like that id remain same for all the keywords which appear in same row as you can see in desired output.

2 Answers

You can try

df["Brand"] = df["Brand"].apply(lambda x: x.split("-"))

print(df.explode("Brand"))

Try the following:

df = df.assign(Brand = df.Brand.str.split('-')).explode('Brand')
print(df)
   ID     Brand
0   1       BMW
1   2        VW
1   2       BMW
1   2      Audi
2   3      Audi
3   4  Mercedes
3   4      Audi
Related