'convert a nested list into dataframes based on row index

I got a nested list with of the shape [<shapely.geometry.point.Point at 0x7fe9da238b80>, <shapely.geometry.polygon.Polygon at 0x7fe9d7469b50>, 0.029849157929994773] with a total of 7332920 entries. I converted this list into a DataFrame and adding column names using:

distances = pd.DataFrame(results)

distances.columns = ['point', 'polygon', 'distance']

point polygon distance
POINT (6.923344699999999 50.9169508) POLYGON Z ((6.9571231 50.9285122 0, 6.957724 5... 0.223

now i want to split it into 40 data frames of size 183323 each. I tried:

lst_distances = [distances.iloc[i:i+183323] for i in range(0,len(distances), 183323)]

But now i don't know how to convert this nested list into 40 different lists or dataframes without going through each manually like this:
d1, d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19, d20, d21,d22,d23, d24,d25,d26,d27,.... = map(list, zip(*lst_distances)

I can't go directly from the dataframe because the polygon item is not hashable.



Solution 1:[1]

What is the problem with the list? You already have lst_distances[0], lst_distances[1],

Using a dictionary is another way but it does not bring any avantage.

d = {str(i+1):e for (i,e) in enumerate(lst_distances)}

defines d['1'], d['2'], ...

Now, if you really want variables:

  1. "manual" solution*

    d1, d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,_
    d21,d22,d23, d24,d25,d26,d27,.... = tuple(lst_distances)
    
  2. Acceding the globals

    for (i,dist) in enumerate(lst_distances):
        globals()[f"d{i+1}"] = dist
    

    Should not be used for locals() by the documentation but would work on most implementations.

  3. Introducing dynamic code with using exec

    exec("\n".join([f"d{i+1} = lst_distances[i]" for i in range(len(lst_distances))]))
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1