Home > Enterprise >  How to explode a Python list of lists of coordinates into separate Pandas columns?
How to explode a Python list of lists of coordinates into separate Pandas columns?

Time:06-13

I have a Python list of lists of coordinates like the one below (let's call it sampled_list):

sampled_list
[[100, 5],
 [200, 1],
 [300, 2],
 [400, 3],
 [500, 9]]

What I would like to obtain is a Pandas DataFrame where its columns correspond to the X and Y coordinates, respectively. Resulting in something like below (let's call it df):

df
|   X   |  Y  |
| ----- | ----|
|  100  |  5  |
|  200  |  1  |
|  300  |  2  |
|  400  |  3  |
|  500  |  9  |

How can this be done? I have tried the .explode() method but with no luck since I get stuck with the resulting multi-index.

CodePudding user response:

You simply need the DataFrame constructor:

df = pd.DataFrame(sampled_list, columns=['X', 'Y'])

output:

     X  Y
0  100  5
1  200  1
2  300  2
3  400  3
4  500  9

used input:

sampled_list = [[100, 5],
                [200, 1],
                [300, 2],
                [400, 3],
                [500, 9]]

CodePudding user response:

pd.DataFrame(lst,columns=['X','Y'])



    X  Y
0  100  5
1  200  1
2  300  2
3  400  3
4  500  9
  • Related