1

Take pandas dataframe as

index           A    
    0       1qwe 3asd
    1       6qwe 35asd
    2       11qwe 13asd
    3       17qwe 8asd
    4       5qwe 9asd
    5       7qwe 2asd
    6       1qwe 20asd

A.dtype = object

Convert this dataframe as follows

index        A    
    0       1.03
    1       6.35
    2       11.13
    3       17.08
    4       5.09
    5       7.02
    6       1.20

A.dtype = float64

Is conversion possible in python? If yes, please give code in efficient way.

After period(.) padding of zero should take place if only one digit is present

2 Answers 2

1

You can do this with one str.replace call,

df['A'].str.replace(r'(\d+).*?(\d+).*', lambda x: '{}.{:0>2}'.format(x[1], x[2]))

pd.to_numeric(df['A'].str.replace(
  r'(\d+).*?(\d+).*', lambda x: '{}.{:0>2}'.format(x[1], x[2])), errors='coerce')

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, please take a look at edited question. After period(.) padding of zero should take place if only one digit is present
@Jayant there was a bug in which numbers with two decimal places weren't handled correctly. I've fixed it now!
1

Try with Series.str.findall, apply.join and pandas.to_numeric:

df['A'] = pd.to_numeric(df.A.str.findall('(\d+)').apply('.'.join))

0     1.30
1     6.35
2    11.13
3    17.80
4     5.90
5     7.20
6     1.20
Name: A, dtype: float64

1 Comment

Thank you, can you please take look at edited question and answer how it can be done?

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.