Formula works for one row does not for two - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Formula works for one row does not for two (/thread-20681.html) |
Formula works for one row does not for two - Prometheus - Aug-25-2019 Hi Trying to extract the core of the problem, I have found that a formula works for one row only, if another row is added, an error shows: cannot convert the series to <class 'float'> By the way, does not work even if the content is turned to float beforehand. So here it is import pandas as pd import numpy as np startlat=51.454234 startlon=-2.593225 df6 = pd.DataFrame(columns=['Latitude', 'Longitude']) df6.loc[0] = [51.3862652, -2.3638941] # df6.loc[1] = [51.3862652, -2.3638941] df6['dist1']=6371.01 * np.arccos(np.sin(np.radians(float(startlat)))*np.sin(np.radians(float(df6['Latitude']))) + np.cos(np.radians(float(startlat)))*np.cos(np.radians(float(df6['Latitude'])))*np.cos(np.radians(float(startlon)) - np.radians(float(df6['Longitude'])))) df6.head()This works. However, when I uncomment the addition of the second row and run this all again, python returns an error: TypeError: cannot convert the series to <class 'float'> What is the cause and how to work around that? Thank you. RE: Formula works for one row does not for two - Prometheus - Aug-25-2019 PS. I am using Collab. The formula in use is proven to work correctly. I have not found any library to deal with that more elegantly. Here are the output of the first go and the full message:
Thank you for your help.
RE: Formula works for one row does not for two - ThomasL - Aug-25-2019 Have a look at this code: import pandas as pd import numpy as np start_lat = 51.454234 start_lon = -2.593225 start_lat = np.radians(start_lat) start_lon = np.radians(start_lon) data = {'latitudes': [51.3862652, 51.4862652, 51.5862652, 51.6862652, 51.7862652, 52.406374], 'longitudes': [-2.3638941, -2.693225, -2.793225, -2.893225, -2.993225, 16.9251681]} df = pd.DataFrame(data) df['latitudes'] = np.radians(df['latitudes']) df['longitudes'] = np.radians(df['longitudes']) R = 6371.01 df['distance'] = R * np.arccos(np.sin(start_lat) * np.sin(df['latitudes']) + np.cos(start_lat) * np.cos(df['latitudes']) * np.cos(start_lon - df['longitudes'])) df RE: Formula works for one row does not for two - Prometheus - Aug-25-2019 Thanks a lot. So, in essence, numpy does not like making the calculation of radians part of the equasion when there are more rows than 1. Would you please be able to explain what makes the difference, and what made you think this is the answer? At first, I thought it had to do with the type of table but this is I think already ruled out by the fact that removing the second row in my example makes it possible to apply the complete formula again. RE: Formula works for one row does not for two - ThomasL - Aug-26-2019 (Aug-25-2019, 08:57 PM)Prometheus Wrote: So, in essence, numpy does not like making the calculation of radians part of the equasion when there are more rows than 1. No, the problem were all these float(df6['Latitude']) etc to change the data type a column (series) in a dataframe you need to call the appropriate pandas methods to do so. You can put the np.radians() back into the calculation and it will work also. |