python - pandas convert strings to float for multiple columns in dataframe -


i'm new pandas , trying figure out how convert multiple columns formatted strings float64's. i'm doing below, seems apply() or applymap() should able accomplish task more efficiently...unfortunately i'm bit of rookie figure out how. values percentages formatted strings '15.5%'

for column in ['field1', 'field2', 'field3']:     data[column] = data[column].str.rstrip('%').astype('float64') / 100 

starting in 0.11.1 (coming out week), replace has new option replace regex, becomes possible

in [14]: df = dataframe('10.0%',index=range(100),columns=range(10))  in [15]: df.replace('%','',regex=true).astype('float')/100 out[15]:  <class 'pandas.core.frame.dataframe'> int64index: 100 entries, 0 99 data columns (total 10 columns): 0    100  non-null values 1    100  non-null values 2    100  non-null values 3    100  non-null values 4    100  non-null values 5    100  non-null values 6    100  non-null values 7    100  non-null values 8    100  non-null values 9    100  non-null values dtypes: float64(10) 

and bit faster

in [16]: %timeit df.replace('%','',regex=true).astype('float')/100 1000 loops, best of 3: 1.16 ms per loop   in [18]: %timeit df.applymap(lambda x: float(x[:-1]))/100 1000 loops, best of 3: 1.67 ms per loop 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -