0

I have a DataFrame with the following columns:

link    sqft    rent    bedroom     address1    address2    address3    bathroom

I have a function that takes 3 addresses as input then returns the latitude and longtitude as a dict.

When I apply the function like this ...

df['lat'] = return_coordinates(df.address1,df.address2,df.addresse3).get('Latitude')

... the lat column contains the same value.

Here's the function:

import herepy

geocoderApi = herepy.GeocoderApi('ap_key')
def return_coordinates(address1,address2,address3):
    response = geocoderApi.free_form('{},{},{}'.format(address1,address2,address3))
    geocode = response.as_dict()
    geocode = geocode.get('Response')
    geocode = geocode.get('View')
    value =  (geocode[0].get('Result')[0].get('Location').get('DisplayPosition'))
    return value
4
  • post more code. Few lines of data and expected result Commented Feb 10, 2020 at 9:05
  • 2
    Try using DataFrame's apply() function and pass your return_coordinates function into it. This will broadcast the results of each set of coordinates to your lat column. Commented Feb 10, 2020 at 9:06
  • @s3dev I don't know how to do that, since the output is based on other columns of the dataset. Commented Feb 10, 2020 at 9:20
  • @KaziAbuJaforJaber: See my answer below. Hope this helps you out. Commented Feb 10, 2020 at 12:21

2 Answers 2

1

Here is a solution using the DataFrame.apply() function. Documentation can be found here.

I've faked you're dataset, albeit poorly, due to a lack of data and access to your API. But this demonstrates how you can use the apply() function to populate your lat column from your function's return value(s).

Basic (hacked) setup:

import pandas as pd

data = {'link': ['www.abc.com/1', 'www.abc.com/2', 'www.abc.com/3'],
        'sqft': [1111, 2222, 3333],
        'rent': ['$1111', '$2222', '$3333'],
        'bedroom': [1, 2, 3],
        'address1': [[34.052235, -118.243683], [33.052235, -117.243683], [32.052235, -115.243683]],
        'address2': [[32.715736, -117.161087], [31.715736, -116.161087], [30.715736, -115.161087]],
        'address3': [[33.541679, -117.777214], [32.541679, -116.777214], [31.541679, -115.777214]],
        'bathroom': [1, 2, 3]}

# Create dataset
df = pd.DataFrame(data)

def return_coordinates(address1, address2, address3):
    """Return the first value of the ``address1`` parameter."""
    return address1[0]

Use the apply function:

This will broadcast the return of your return_coordinates function to your DataFrame's lat column.

df['lat'] = df.apply(lambda x: return_coordinates(x['address1'], x['address2'], x['address3']), axis=1)

Original dataset:

    address1    address2    address3    bathroom    bedroom     link    rent    sqft
0   [34.052235, -118.243683]    [32.715736, -117.161087]    [33.541679, -117.777214]    1   1   www.abc.com/1   $1111   1111
1   [33.052235, -117.243683]    [31.715736, -116.161087]    [32.541679, -116.777214]    2   2   www.abc.com/2   $2222   2222
2   [32.052235, -115.243683]    [30.715736, -115.161087]    [31.541679, -115.777214]    3   3   www.abc.com/3   $3333   3333

With the new lat column:

address1    ...     lat
0   [34.052235, -118.243683]    ...     34.052235
1   [33.052235, -117.243683]    ...     33.052235
2   [32.052235, -115.243683]    ...     32.052235

As you can see in the results, the apply function returns the first value of the address1 field, for each row.

Sign up to request clarification or add additional context in comments.

1 Comment

Seem's like it would work with a little adjustment's, I'll be right back after I see If I can get it to work.
1

Your code should work. Here is an example of what you are looking for, just in case :

df = pd.DataFrame({'address1': [1, 2, 3], 'address2': [4, 5, 6], 'address3': [7, 8, 9]})

def return_coordinates(a,b,c):
    d = {
        "Latitude": a+b+c,
        "Longitude": a*b*c
    }
    return d

df['lat'] = return_coordinates(df['address1'], df['address2'], df['address3']).get("Latitude")
df['lng'] = return_coordinates(df['address1'], df['address2'], df['address3']).get("Longitude")
print(df)

Output :

   address1  address2  address3  lat  lng
0         1         4         7   12   28
1         2         5         8   15   80
2         3         6         9   18  162

6 Comments

df['lat'] = rc(df['address1'],df['address2'],df['address3']).get('Latitude') df['lng'] = rc(df['address1'],df['address2'],df['address3']).get('Longitude') - Doesn't work Same problme
I edited my answer. Is it still not the same problem ?
Yep, same problem, every row in lat and lng column is the same
maybe try to put the line geocoderApi = herepy.GeocoderApi('ap_key') inside your return_coordinates function
weird problem ^^. Maybe you could check the type of value and see if it's an immutable item
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.