2

In the below code, the dataframe df5 is not getting populated. I am just assigning the values to dataframe's columns and I have specified the column beforehand. When I print the dataframe, it returns an empty dataframe. Not sure whether I am missing something.

Any help would be appreciated.

import math    
import pandas as pd

columns = ['ClosestLat','ClosestLong']

df5 = pd.DataFrame(columns=columns)

def distance(pt1, pt2):
  return math.sqrt((pt1[0] - pt2[0])**2 + (pt1[1] - pt2[1])**2)

for pt1 in df1:
   closestPoints = [pt1, df2[0]]
   for pt2 in df2:
     if distance(pt1, pt2) < distance(closestPoints[0], closestPoints[1]):
       closestPoints = [pt1, pt2]
       df5['ClosestLat'] = closestPoints[1][0]
   df5['ClosestLat'] = closestPoints[1][0]
   df5['ClosestLong'] = closestPoints[1][1]
   print ("Point: " + str(closestPoints[0]) + " is closest to " + str(closestPoints[1]))
1
  • Please define df1 and df2. Commented Aug 21, 2016 at 1:06

1 Answer 1

3

From the look of your code, you're trying to populate df5 with a list of latitudes and longitudes. However, you're making a couple mistakes.

  1. The columns of pandas dataframes are Series, and hold some type of sequential data. So df5['ClosestLat'] = closestPoints[1][0] attempts to assign the entire column a single numerical value, and results in an empty column.
  2. Even if the dataframe wasn't ignoring your attempts to assign a real number to the column, you would lose data because you are overwriting the column with each loop.

The Solution: Build a list of lats and longs, then insert into the dataframe.

import math    
import pandas as pd

columns = ['ClosestLat','ClosestLong']

df5 = pd.DataFrame(columns=columns)

def distance(pt1, pt2):
  return math.sqrt((pt1[0] - pt2[0])**2 + (pt1[1] - pt2[1])**2)

lats, lngs = [], []
for pt1 in df1:
   closestPoints = [pt1, df2[0]]
   for pt2 in df2:
     if distance(pt1, pt2) < distance(closestPoints[0], closestPoints[1]):
       closestPoints = [pt1, pt2]
   lats.append(closestPoints[1][0])
   lngs.append(closestPoints[1][1])

df['ClosestLat'] = pd.Series(lats)
df['ClosestLong'] = pd.Series(lngs)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.