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.
apply()function and pass yourreturn_coordinatesfunction into it. This will broadcast the results of each set of coordinates to yourlatcolumn.