8

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!

3
  • 10
    Are you after geo.values.tolist()? Commented Oct 22, 2016 at 22:38
  • @MaxU pundit worthy Commented Oct 23, 2016 at 0:23
  • @piRSquared I agree Commented Nov 10, 2016 at 10:09

2 Answers 2

6

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]]
Sign up to request clarification or add additional context in comments.

Comments

-1

How about

out_list = []
for index, row in geo.iterrows():
    out_list.append([row.lat, row.long])

3 Comments

thanks! that is more concise! @MaxU's was even more concise though.
Note: .iterrows() is notoriously sluggish, use it only if absolutely necessary.
Agreed - I've learnt a lot more about pandas in the years since and the accepted answer is far, far better.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.