Home > database >  python pandas how to extract number from string?
python pandas how to extract number from string?

Time:10-11

I have an dataframe and I want to extract only number from string and appoint theme another new row.

dataframe:

 type
gp250,sp280

expected result for dataframe:

type   price
gp      25
sp      280

CodePudding user response:

You can use a regular expression to extract the consecutive digits of the string and then explode to transform the result into multiple rows.

import re
df["type"].apply(lambda x: re.findall("([0-9] )", x)).explode()

CodePudding user response:

Assuming you always have the pattern with non digit characters followed by numbers, you can first split and explode joined string into separate rows and then use .str.extract to extract non digit pattern (\D*) and number (\d*) into different columns:

df['type'].str.split(',').explode().str.extract('(?P<type>\D*)(?P<price>\d*)', expand=True)
  type price
0   gp   250
0   sp   280
  • Related