Stack Overflow em Português Asked by Alan Fros on November 27, 2021
boa tarde.
Estou com um problema em uma atividade do AceleraDev em Data Science, gostaria de um auxílio.
Quando chamo a função .info(), ele exibe os valores abaixo:
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
Aqui estou tentando converter algumas colunas para numericos e substituir os separadores decimais com esses comandos abaixo:
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')
Gostaria de saber se há uma forma mais prática de fazer essa conversão.
Obrigado.
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'))
Answered by Cairo Rocha on November 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP