Python Forum
Formula works for one row does not for two
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formula works for one row does not for two
#1
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.
Reply


Messages In This Thread
Formula works for one row does not for two - by Prometheus - Aug-25-2019, 02:45 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020