How to convert multiple columns from Object to Numeric in pandas?

Asked

Viewed 258 times

-1

good afternoon. I have a problem in an Aceleradev activity in Data Science, I would like a help.

When I call the function . info(), it displays the values below:

 countries.info()

    <class 'pandas.core.frame.DataFrame'>
RangeIndex: 227 entries, 0 to 226
Data columns (total 20 columns):
 #   Column            Non-Null Count  Dtype  
---  ------            --------------  -----  
 0   Country           227 non-null    object 
 1   Region            227 non-null    object 
 2   Population        227 non-null    int64  
 3   Area              227 non-null    int64  
 4   Pop_density       227 non-null    object 
 5   Coastline_ratio   227 non-null    object 
 6   Net_migration     224 non-null    object 
 7   Infant_mortality  224 non-null    object 
 8   GDP               226 non-null    float64
 9   Literacy          209 non-null    object 
 10  Phones_per_1000   223 non-null    object 
 11  Arable            225 non-null    object 
 12  Crops             225 non-null    object 
 13  Other             225 non-null    object 
 14  Climate           205 non-null    object 
 15  Birthrate         224 non-null    object 
 16  Deathrate         223 non-null    object 
 17  Agriculture       212 non-null    object 
 18  Industry          211 non-null    object 
 19  Service           212 non-null    object 
dtypes: float64(1), int64(2), object(17)
memory usage: 35.6+ KB

Here I am trying to convert some columns to numeric and replace the decimal separators with these commands below:

countries['Pop_density'] = pd.to_numeric(countries['Pop_density'].str.replace(',','.'))
countries['Coastline_ratio'] = pd.to_numeric(countries['Coastline_ratio'].str.replace(',','.'))
countries['Net_migration'] = pd.to_numeric(countries['Net_migration'].str.replace(',','.'),errors='coerce')
countries['Infant_mortality'] = pd.to_numeric(countries['Infant_mortality'].str.replace(',','.'),errors='coerce')
countries['Literacy'] = pd.to_numeric(countries['Literacy'].str.replace(',','.'),errors='coerce')
countries['Phones_per_1000'] = pd.to_numeric(countries['Phones_per_1000'].str.replace(',','.'),errors='coerce')
countries['Arable'] = pd.to_numeric(countries['Arable'].str.replace(',','.'),errors='coerce')
countries['Crops'] = pd.to_numeric(countries['Crops'].str.replace(',','.'),errors='coerce')
countries['Other'] = pd.to_numeric(countries['Other'].str.replace(',','.'),errors='coerce')
countries['Climate'] = pd.to_numeric(countries['Climate'].str.replace(',','.'),errors='coerce')
countries['Birthrate'] = pd.to_numeric(countries['Birthrate'].str.replace(',','.'),errors='coerce')
countries['Deathrate'] = pd.to_numeric(countries['Deathrate'].str.replace(',','.'),errors='coerce')
countries['Agriculture'] = pd.to_numeric(countries['Agriculture'].str.replace(',','.'),errors='coerce')
countries['Industry'] = pd.to_numeric(countries['Industry'].str.replace(',','.'),errors='coerce')
countries['Service'] = pd.to_numeric(countries['Service'].str.replace(',','.'),errors='coerce')

I wonder if there is a more practical way to make this conversion.

Thank you.

1 answer

0


columns = ['Pop_density', 'Coastline_ratio', 'Net_migration', 'Infant_mortality', 'Literacy', 'Phones_per_1000', 'Arable', 'Crops', 'Other', 'Climate', 'Birthrate',  'Deathrate', 'Agriculture', 'Industry', 'Service'] 

countries[columns] = countries[columns].apply(lambda x: x.str.replace(',', '.').astype('float'))
  • Thanks friends, solved here!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.