List float number formatting

Asked

Viewed 126 times

-1

I have a dataframe with Latitude and Longitude columns, in string format

Atibaia['LATITUDE'].head()

140     -231,124,852,387,647
245     -231,159,749,902,692
254          -23,116,747,205
512     -231,560,912,572,211
1348         -23,115,763,607

I was able to remove the commas and convert to float using the following code snippet:

Atibaia['LATITUDE'] = Atibaia.loc[:,'LATITUDE'].str.replace(',','').astype(float)
Atibaia['LONGITUDE'] = Atibaia.loc[:,'LONGITUDE'].str.replace(',','').astype(float)

(That even generates the following warning: C: Users Ramon Anaconda3 lib site-Packages ipykernel_launcher.py:2: Settingwithcopywarning: A value is trying to be set on a copy of a Slice from a Dataframe. Try using . Loc[row_indexer,col_indexer] = value Instead

See the caveats in the Documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#Indexing-view-versus-copy)

but it works and I end this result:

Atibaia['LATITUDE'].head()

140    -2.311249e+14
245    -2.311597e+14
254    -2.311675e+10
512    -2.315609e+14
1348   -2.311576e+10

However, I need the values in this format -> -23.1171, -46.5502 (Latitude x Longitude of Atibaia)

How should I proceed now? I’ve been 'stuck' in this for a week and I can’t find a way around it

1 answer

0

After a few days I found an answer, follow:

Atibaia['LATITUDE'] = atibaia.LATITUDE.str.replace(',','')
Atibaia['LATITUDE'] = atibaia['LATITUDE'].apply(pd.to_numeric) # <--- THIS!!!!!!
Atibaia['LATITUDE'].head()

Browser other questions tagged

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