Home > Enterprise >  Extract comma separated number from str using pandas [duplicate]
Extract comma separated number from str using pandas [duplicate]

Time:10-05

I have a dataframe in which one column has the following type of values

df['description']

Out of the 7,700 cats rafiq adopted white cats.
Out of the 1,100 dogs sam adopted black cats.
Out of the 2,300 fish sam adopted black cats.

i want to extract the numbers to a new column such as:

df['new column']

7700
1100
2300

CodePudding user response:

You could use a regular expression to remove anything that is not a number and then convert the result to a number.

df['value'] = df.description.str.replace(r'\D ', '').astype('double')

This will give you:

                                       description  value
0   Out of the 7,700 cats rafiq adopted white cats. 7700.0
1   Out of the 1,100 dogs sam adopted black cats.   1100.0
2   Out of the 2,300 fish sam adopted black cats.   2300.0
  • Related