[Python] Quickest way to create a Shapely Polygon from a DataFrame

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a geometry column (Polygons) from a DataFrame that contains the following columns: xmin, xmax, ymin, ymax.

    This is what my DataFrame (df) looks like:

    xmin xmax ymin ymax
    0 -715668.153951 -715418.153951 6.419357e+06 6.419607e+06
    1 -715668.153951 -715418.153951 6.419357e+06 6.419607e+06
    2 -715668.153951 -715418.153951 6.419357e+06 6.419607e+06
    3 -715668.153951 -715418.153951 6.419357e+06 6.419607e+06
    4 -715668.153951 -715418.153951 6.419357e+06 6.419607e+06


    I've managed to do what I want with the following code:

    def polygonise_2Dcells(df_row):
    return Polygon([(df_row.xmin, df_row.ymin), (df_row.xmax, df_row.ymin),(df_row.xmax, df_row.ymax), (df_row.xmin, df_row.ymax)])


    and then:

    polys = df.apply(polygonise_2Dcells, axis=1)


    While the above code works fine, 'apply' is rather slow for my needs (very big dataset). Thus, I am looking for a quicker way to achieve the desired result.

    Continue reading...

Compartilhe esta Página