Issue
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
Solution
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]
>>>
Answered By - U12-F̉͋̅̾̇orward
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.