1

I need to extract numeric values from a string inside a pandas DataFrame.

Let's say the DataFrame cell is as follows (stored as a string):

[1.234,2.345]

I can get the first value with the following:

print(df['column_name'].str.extract('(\d+.\d+)',).astype('float'))

Output:

1.234

Now my thoughts to find both values was to do the following:

print(df['column_name'].str.extract('(\d+.\d+),(\d+.\d+)',).astype('float'))

but the output is then as follows:

NaN NaN

Expected output:

1.234 2.345
2
  • Looks like there is a space after your comma there. df['column_name'].str.extract('(\d+.\d+)\,\s(\d+.\d+)',) Commented Sep 20, 2021 at 23:15
  • @Chris df['column_name'].str.extract('(\d+.\d+)\,\s(\d+.\d+)',) still results in NaN Nan Commented Sep 20, 2021 at 23:19

3 Answers 3

1

Why not just pd.eval:

>>> df['Float'] = pd.eval(df['String'])
>>> df
           String           Float
0  [1.234, 2.345]  [1.234, 2.345]
1  [1.234, 2.345]  [1.234, 2.345]
>>> 
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use a regex to extract floats, you can use str.findall:

>>> df['column_name'].str.findall(r'(-?\d+\.?\d+)').str.join(' ')
0    1.234 2.345
Name: String, dtype: object

Old answer: Use ast.literal_eval:

import ast

df = pd.DataFrame({'String': ['[1.234, 2.345]']})

df['Float'] = df['String'].apply(ast.literal_eval)

Output:

>>> df
           String           Float
0  [1.234, 2.345]  [1.234, 2.345]

>>> type(df.at[0, 'String'][0])
str

>>> type(df.at[0, 'Float'][0])
float

1 Comment

@Hellothere. Does it solve your problem?
0

You can use pandas.str.split, setting n=2. If you want to expand the DataFrame you must set expand=True. So the result might look like:

your_dataframe['your_column_name'].str.split(",", n=2, expand=True).astype(float)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.