Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I solve this double sub-string and astype problem

In this case I have a problem with this:
Im trying to split a column "Ubicación Finalizada" enter image description here in two, but I have a problem with a "," string, I can’t take it out and that make some problems in the after code because I can’t convert it to float in the "Latitud" column with the ‘Longitud’ I dont have problem.

Code:

  df['Latitud'] = df['Ubicación finalizada'].str.slice(0, 12).astype(str)
df["Latitud"] = df["Latitud"].replace(",", "" )
df['Latitud'] = df['Latitud'].astype('float')
df['Longitud'] = df['Ubicación finalizada'].str.slice(12, 25)
df['Longitud1'] = df['Longitud'].astype('float')
df['Cliente'] = df['Cliente'].astype('str')
df['Conductor'] = df['Conductor'].astype('str')
df['Zona de tarifa'] = df['Zona de tarifa'].str.slice(4, 7)
df["Zona de tarifa1"] = 'Zona ' + df['Zona de tarifa'].astype('str') + ' $'+ df['Precio'].astype('str') + '      Conductor: ' + df['Conductor'].astype('str')
colors= ['lightgray', 'darkred', 'lightgreen', 'green', 'pink', 'darkgreen', 'lightblue', 'darkpurple', 'black', 
'lightred', 'gray', 'orange', 'cadetblue', 'blue', 'purple', 'beige', 'white', 'darkblue', 'red']

map = folium.Map(location=[df.Latitud.mean(), df.Longitud.mean()], zoom_start=14, control_scale=True)
for index, location_info in df.iterrows():
    folium.Marker([location_info["Latitud"], location_info["Longitud"]],icon=folium.Icon(color=colors[int(location_info['Zona de tarifa'])]),popup=location_info["Cliente"]+' '+location_info["Zona de tarifa1"]).add_to(map)
map

'''


        Error:
ValueError                                Traceback (most recent call last)
<ipython-input-136-ccd19cd31b4c> in <module>
      1 df['Latitud'] = df['Ubicación finalizada'].str.slice(1, 12).astype(str)
      2 df["Latitud"] = df["Latitud"].replace(",", ",0" )
----> 3 df['Latitud'] = df['Latitud'].astype('float')
      4 df['Longitud'] = df['Ubicación finalizada'].str.slice(12, 25)
      5 df['Longitud1'] = df['Longitud'].astype('float')

6 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
1199 if copy or is_object_dtype(arr.dtype) or is_object_dtype(dtype):
1200 # Explicit copy, or required since NumPy can’t view from / to object.
-> 1201 return arr.astype(dtype, copy=True)
1202
1203 return arr.astype(dtype, copy=copy)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ValueError: could not convert string to float: ‘34.9051275,’

Also I want to convert "# Externo" that is in the format 4.176466e+10   - to normal numbers. 

>Solution :

You can use .str.split to split the string column and then convert it:

df[["lat", "lon"]] = df["Ubicación finalizada"].str.split(",", expand=True)
df[["lat", "lon"]] = df[["lat", "lon"]].astype(float)
print(df)

Prints:

  Ubicación finalizada       lat     lon
0      -34.123,-56.156 -34.12300 -56.156
1     -35.1234,-57.156 -35.12340 -57.156
2    -36.12356,-58.156 -36.12356 -58.156

df used:

  Ubicación finalizada
0      -34.123,-56.156
1     -35.1234,-57.156
2    -36.12356,-58.156
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading