split an enumerated text list into multiple columns

2024/7/6 21:52:59

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

Answer

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

https://en.xdnf.cn/q/120508.html

Related Q&A

Python using self as an argument [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

how to shuffle questions in python

please anyone help me i have posted my whole work down can anyone tell me how to shuffle theses five questions it please i will be very thankful.print("welcome to the quiz") Validation = Fals…

Selenium python do test every 10 seconds

I am using selenium (python) testing and I need to test my application automatically every 10 seconds.How can I do this?

Type Object has no attribute

I am working on a program, but I am getting the error "Type object Card has no attribute fileName. Ive looked for answers to this, but none that Ive seen is in a similar case to this.class Card: R…

Generate random non repeating samples from an array of numbers

I made a battleships game and I now need to make sure that the computer doesnt attack at the same spot twice.My idea of it is storing each shots co-ordinates in a variable which gets added to whenever …

How to get back fo first element in list after reaching the end? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Collision detection on the y-axis does not work (pygame)

I am trying to get the collision detection in my code to work. I am using vectors and I want the player sprite to collide and stop when it collides with a sprite group called walls. The problem is that…

Search for string within files of a directory

I need help writing a light weight Python (v3.6.4) script to search for a single keyword within a directory of files and folders. Currently, I am using Notepad++ to search the directory of files, altho…

Extract parent and child node from python tree

I am using nltks Tree data structure.Below is the sample nltk.Tree.(S(S(ADVP (RB recently))(NP (NN someone))(VP(VBD mentioned)(NP (DT the) (NN word) (NN malaria))(PP (TO to) (NP (PRP me)))))(, ,)(CC an…

Click button with selenium and python

Im trying to do web scraping with python on and Im having trouble clicking buttons. Ive tried 3 different youtube videos using Xpath, driver.find_element_by_link_text, and driver.find_element. What am …