python - Converting some columns from pandas dataframe to list of lists -


i have dataframe. of data converted list of list. columns i'm interested in index, name, , births. code works, seems inefficient , reason letter l added end of each index.

my code:

import pandas pd   data = [['bob', 968, 'male'], ['jessica', 341, 'female'], ['mary', 77, 'female'], ['john', 578, 'male'], ['mel', 434, 'female']] headers = ['names', 'births', 'gender'] df = pd.dataframe(data = data, columns=headers) indexes = df.index.values.tolist() mylist =  [[x] x in indexes]  x in mylist:     x.extend([df.ix[x[0],'names'], df.ix[x[0],'births']])  print mylist 

desired output:

[[0, 'bob', 968], [1, 'jessica', 341], [2, 'mary', 77], [3, 'john', 578], [4, 'mel', 434]] 

why not use .values.tolist() mentioned?

import pandas pd  # data # ================================================= data = [['bob', 968, 'male'], ['jessica', 341, 'female'], ['mary', 77, 'female'], ['john', 578, 'male'], ['mel', 434, 'female']] headers = ['names', 'births', 'gender'] df = pd.dataframe(data = data, columns=headers)  # nested list # ============================ df.reset_index()[['index', 'names', 'births']].values.tolist()  out[46]:  [[0, 'bob', 968],  [1, 'jessica', 341],  [2, 'mary', 77],  [3, 'john', 578],  [4, 'mel', 434]] 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -