0

df =

  lat/long0         lat/long1       lat/long2       lat/long3   
1 45.1024 7.7498    45.1027 7.75    45.1072 7.7568  45.1076 7.7563
2 45.0595 7.6829    45.0595 7.6829  45.0564 7.682   45.0533 7.6796

My DataFrame is str(string) type. Merged latitude and longitude with 'space'. And when I want to do some math operations I get the Value Error: could not convert string to float.

Namely, I want to take difference between rows by taking into account the order of latitude and longitude:

lat/long0  
45.1024-45.0595 7.7498-7.6829

Actually I will calculate distances (ex. euclidean, manhattan distance and etc.) and main problem is to change the type to type able to do math

Mamed
  • 1,102
  • 8
  • 23
  • 1
    You are getting the error because a space is not part of any valid number. Take a minute to think through how you can deal with this problem. – Code-Apprentice Aug 05 '19 at 16:01
  • @Code-Apprentice Actually the question is how to deal with what you have said just know – Mamed Aug 05 '19 at 16:05
  • You can't do math on strings, so you need to convert them to numbers. [This question](https://stackoverflow.com/questions/14745022/how-to-split-a-column-into-two-columns) and [this function](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_numeric.html) might be a good place to start. – jirassimok Aug 05 '19 at 16:07
  • @jirassimok I have these values separately. I merged them by myself. Because I need to calculate Euclidean distance of a point (long and lat), but not single lat and single long – Mamed Aug 05 '19 at 16:18
  • The data underlying Pandas' DataFrame is a Numpy array, so take a look at the tools available for those. You can use [`scipy.spatial.distance.euclidean`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.euclidean.html) (or [`numpy.linalg.norm`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html)) to calculate the distance between vectors. Scipy has [a wealth of distance-related functions](https://docs.scipy.org/doc/scipy/reference/spatial.distance.html), which you may find helpful as well. – jirassimok Aug 05 '19 at 16:25
  • @Mamed There are two problems: 1. You cannot do math with a string. You need to keep the separate lat and long values in order to do the distance calculations. 2. Euclidean distance is for (x, y) coordinates in a flat plane. However, lat/long coordinates are on the surface of an (approximate) sphere. I suggest that you research the formula for calculating distance between lat/long pairs. – Code-Apprentice Aug 05 '19 at 16:26
  • 1
    @jirassimok Does scipy also have a function for calculating spherical distances? This is what needed here, not euclidean distance. (Note: I'm not the OP. I'm asking to be sure we are pushing the OP in the right direction.) – Code-Apprentice Aug 05 '19 at 16:27
  • @jirassimok Ok, Euclidean was a sample to say. Actually I`m calculation Frechet distance and there is no library for that – Mamed Aug 05 '19 at 16:32

1 Answers1

2

As per the comment, a whitespace (ord 32) is not a number. You can split the string at the whitespace with:

string_with_space = "45.1024-45.0595 7.7498-7.6829"
latitude, longitude = string_with_space.split(" ")

this way, latitude=45.1024-45.0595 and longitude=7.7498-7.6829

Wolfeius
  • 303
  • 2
  • 14
  • From the comments, it sounds like the OP has the separate lat/long values already and concatenated them unnecessarily into a string. – Code-Apprentice Aug 05 '19 at 16:28