Create nested list from Pandas dataframe

pandas, python

Solution

`pandas` is built on top of `numpy`. A `DataFrame` stores its `values` in a `numpy` array, which has a `tolist` method.

>>> geo = pd.DataFrame({'lat': [40.672304, 40.777169, 40.712196],
    ...:                 'lon': [-73.935385, -73.988911, -73.957649]})
    ...:
>>> geo.values
>>>
array([[ 40.672304, -73.935385],
       [ 40.777169, -73.988911],
       [ 40.712196, -73.957649]])
>>>
>>> geo.values.tolist()
[[40.672304, -73.935385], [40.777169, -73.988911], [40.712196, -73.957649]]

Problem

i have a simple pandas dataframe with two columns. i would like to generate a nested list of those two columns. ``` geo = pd.DataFrame({'lat': [40.672304, 40.777169, 40.712196], 'lon': [-73.935385, -73.988911, -73.957649]}) ``` my solution to this problem is the following: ``` X = [[i] for i in geo['lat'].tolist()] Y = [i for i in geo['lon'].tolist()] for key, value in enumerate(X): X[key].append(Y[key]) ``` however, i feel there must be a better way than this. thanks!

Original source