Search code examples
pythonpandasapplygeopandas

Python: how to properly use apply()?


I have a geopandas dataframe that contains points

df:

        geometry
    0   POINT (806470.3646198167 2064879.919354021)
    1   POINT (792603.391127742 2170760.8355139)
    2   POINT (787263.3037740411 2050925.953643546)
    3   POINT (809203.6762813283 2160874.194588484)
    4   POINT (781668.2687635225 2051524.634389534)

for each point I would like to apply this function:

def returnValuePoints(df, i):
    points_list = [ (df['geometry'][i].x, df['geometry'][i].y) ] #list of X,Y coordinates
    for point in points_list:
        col = int((point[0] - xOrigin) / pixelWidth)
        row = int((yOrigin - point[1] ) / pixelHeight)
        return (row, col)

this what I am doing:

for i in df.index:
    val = returnValuePoints(df, data, i)

How can I avoid the loop and apply this function to all the rows with apply()


Solution

  • First you need to restructure your method so it takes values which you can pass using the lambda function within the apply (think of how you would want to operate on the values of an individual row):

    def returnValuePoints(x, y):
        point = (x, y)
        col = int((point[0] - xOrigin) / pixelWidth)
        row = int((yOrigin - point[1] ) / pixelHeight)
        return (row, col)
    

    Second you can call apply on the dataframe using axis=1 to be able to access the column values of each row:

    val = df.apply(lambda x: returnValuePoints(x.geometry.x, x.geometry.y), axis=1)