2021年4月5日星期一

Extract numbers from strings for several columns in python

Using str.extract() to extract numbers from strings works but it's time-consuming when I have to this on 30 columns. Is there a more efficient way to do this?

Sample input:

df['dips_cert_earning_premium_weekly'] = df['dips_cert_earning_premium_weekly'].str.extract(r'(\d+(?:\.\d+)?)').astype(float)  

Sample output:

yyyy  2014    110.743802  2015    103.753351  2016    111.653117  2017           NaN  2018           NaN  Name: dips_cert_earning_premium_weekly, dtype: float64  
https://stackoverflow.com/questions/66961860/extract-numbers-from-strings-for-several-columns-in-python April 06, 2021 at 10:06AM

没有评论:

发表评论