1

With reference to this, I wanted to explode the dataframe into separate columns by splitting the values into newlines as separate rows. The values may contain multiple new lines.

Example Dataframe:

Fruit  |  Store
----------------
Apple  |  Querty
Banana |  Mouz\nVazhai
Grapes |  Angoor\nRangoon

I want this dataframe to be split into the following:

Fruit  |  Store
----------------
Apple  |  Querty
Banana |  Mouz
Banana |  Vazhai
Grapes |  Angoor
Grapes |  Rangoon

Is there any solution to accomplish this?

1 Answers1

1

First split values to lists and then use DataFrame.explode:

df = df.assign(Store = df['Store'].str.split(r'\n')).explode('Store')
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
  • All this code does is convert that text into a list and place it in a new column. I wanted to create the same row again with just the Store values as unique. – Mujeebur Rahman Mar 02 '22 at 10:13