-3

I have a dataframe column which is a an enumerated list of items and I'm trying to split them into multiple columns. for example

dataframe column that looks like this:

ID ITEMS
ID_1 1. Fruit 12 oranges 2. vegetables 7 carrot 3. NFL 246 SHIRTS
ID_2 1. Rock 2000 tons 2. PAPER 7 Notebook 3. Scissors 246 pairs

the preferred result is something like this:

ID Fruit vegetables NFL Rock PAPER Scissors
ID_1 12 oranges 7 carrot 0 0 0 0
ID_2 0 0 246 SHIRTS 2000 tons 7 Notebook 246 pairs

Also the number of the items varies from row to another [2-7] I'm trying to use str.extract

 df['ITEMS'].str.extract('(\d\.+)', flags=re.M, expand=True)

but it didn't work

Robert
  • 7,394
  • 40
  • 45
  • 64

1 Answers1

0

this codes works with me

df.str.split(pat="\d\.\s", expand=True)

this one breaks it down separating the numbers and the rest of the text

  • this line of code doesn't work for me. `'DataFrame' object has no attribute 'str'` I guess you mean `df['ITEMS']` but still, the output i get is this: `0 1 2 3 ID_1 Fruit 12 oranges vegetables 7 carrot NFL 246 SHIRTS ID_2 Rock 2000 tons PAPER 7 Notebook Scissors 246 pairs` You have the name for your columns and its values still in one cell... – Rabinzel Mar 04 '22 at 19:32