Skip to main content
1 of 2
Pieter
  • 4.6k
  • 1
  • 11
  • 26

You can use dissolve to group on a column and create a union of the geometry column.

Next, you can convert the unioned points to linestrings.

Code sample:

import geopandas as gpd
import shapely
from shapely import Point

input = gpd.GeoDataFrame(
    {
        "userid": [1, 2, 2, 3, 3], 
        "geometry": [
            Point(0,0), Point(1,0), Point(1,1), Point(2,0), Point(2,1)
        ],
    },
    crs="EPSG:31370",
)

diss = input.dissolve(by="userid")
diss_multi = diss.loc[diss.geometry.type == "MultiPoint"]

print(diss)

lines = diss_multi.set_geometry(
    diss_multi.geometry.apply(lambda x: shapely.LineString(shapely.get_coordinates(x)))
)

print(lines)

Result:

userid
2       LINESTRING (1 0, 1 1)
3       LINESTRING (2 0, 2 1)

PS: it is always easier if you include a runnable minimal reproducible example in code including input data. Based on how I understood your question I tried to create "runnable" sample data in the code sample.

Pieter
  • 4.6k
  • 1
  • 11
  • 26