How to convert a list with special delimiter to a python dataframe?

Question:

I have the following list where double brackets indicate a new element:

[[[33.79277702, -104.3900481],
  [35.79415582, -104.39016576],
  [38.7939, -107.31792],
  [31.792589, -188.38847],
  [36.79221, -108.388367],
  [36.79238003, -108.38905313]],
 [[38.1726905, -54.85042496],
  [30.179095, -84.88893],
  [36.17621409, -84.78],
  [39.17534035, -84.8481921],
  [31.17427369, -84.8499793],
  [50.17466907, -84.8578298]],
 [[46.71949073, -109.69390116],
  [46.72091429, -109.69484574],
  [46.72077, -107.69432],
  [46.7199, -107.6916]],
 [[43.60399, -76.963267], [43.60534111, -79.96221766], [43.6049, -78.9613]],
 [[41.93863726, -81.33993917],
  [43.93630951, -81.33862768],
  [43.9369507, -81.33917589]],
 [[12.19490918, -103.10334755],
  [34.19538203, -124.10439655],
  [22.19548313, -194.10379399],
  [22.19505863, -194.10286483]],
 [[38.99843815, -107.81278381],
  [38.99904541, -107.81251648],
  [38.99930408, -107.81234494],
  [32.99882888, -102.81252263]],
 [[36.3735, -161.8463], [36.3741, -161.8481], [36.374, -161.8466]]]

I would like to convert this list into the following dataframe model (where each list element corresponds to a dataframe row).

  polygon 
[[[[33.79277702, -104.3900481],[35.79415582,-104.39016576],
[38.7939, -107.31792], [31.792589, -188.38847],
[36.79221, -108.388367],[36.79238003, -108.38905313]]]
      
[[[38.1726905, -54.85042496],[30.179095, -84.88893],
[36.17621409, -84.78],[39.17534035, -84.8481921],
[31.17427369, -84.8499793],[50.17466907, -84.8578298]]]
           ...

How can I indicate that the list has to be break by double bracket instead of traditional coma ?

Asked By: JEG

||

Answers:

If I get your question, you can transform your list of lists to dict:

polygon = [[[33.79277702, -104.3900481], [35.79415582, -104.39016576],
            [38.7939, -107.31792], [31.792589, -188.38847],
            [36.79221, -108.388367], [36.79238003, -108.38905313]],
           [[38.1726905, -54.85042496], [30.179095, -84.88893],
            [36.17621409, -84.78], [39.17534035, -84.8481921],
            [31.17427369, -84.8499793], [50.17466907, -84.8578298]],
           [[46.71949073, -109.69390116], [46.72091429, -109.69484574],
            [46.72077, -107.69432], [46.7199, -107.6916]],
           [[43.60399, -76.963267], [43.60534111, -79.96221766],
            [43.6049, -78.9613]],
           [[41.93863726, -81.33993917], [43.93630951, -81.33862768],
            [43.9369507, -81.33917589]],
           [[12.19490918, -103.10334755], [34.19538203, -124.10439655],
            [22.19548313, -194.10379399], [22.19505863, -194.10286483]],
           [[38.99843815, -107.81278381], [38.99904541, -107.81251648],
            [38.99930408, -107.81234494], [32.99882888, -102.81252263]],
           [[36.3735, -161.8463], [36.3741, -161.8481], [36.374, -161.8466]]]
df = pd.DataFrame({'polygon': polygon})
    polygon
0   [[33.79277702, -104.3900481], [35.79415582, -1...
1   [[38.1726905, -54.85042496], [30.179095, -84.8...
2   [[46.71949073, -109.69390116], [46.72091429, -...
3   [[43.60399, -76.963267], [43.60534111, -79.962...
4   [[41.93863726, -81.33993917], [43.93630951, -8...
5   [[12.19490918, -103.10334755], [34.19538203, -...
6   [[38.99843815, -107.81278381], [38.99904541, -...
7   [[36.3735, -161.8463], [36.3741, -161.8481], [...
Answered By: ErnestBidouille