0

I have data set of Ecommerce Bookstore in which there is a column Book name containing the name of the books purchased by customers in a single column (separated by , & /). I want to divide the Books into different columns. For example Book1, Book2 Book3, etc.

I know this is do-able using

df[['Book1','Book2']] = df['Book Name'].apply( lambda x: pd.Series(str(x).split("/")))

But I don't know how many columns do I need to separate them.

Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
Asra Khalid
  • 177
  • 1
  • 18
  • Sample input and output please – Subbu VidyaSekar Jan 22 '21 at 10:31
  • Does this answer your question? [Split column into unknown number of columns according to number of words- Pandas](https://stackoverflow.com/questions/62635214/split-column-into-unknown-number-of-columns-according-to-number-of-words-pandas) – r.burak Jan 22 '21 at 10:34
  • 1
    From the dupes - just more concrete: `df = pd.concat([df, df["Book Name"].str.split(r"[,/]", expand=True)], axis=1)` using a regex to split your existing column and concatting it back to your df - afterwards do a column renaming (see f.e. https://stackoverflow.com/questions/11346283/renaming-columns-in-pandas) - the trick here is to use the `expand=True` param. – Patrick Artner Jan 22 '21 at 10:49

0 Answers0