I have a dataframe. I would like some of the data to be converted to a list of list. The columns I'm interested in are the index, Name, and Births. My code works, but it seems inefficient and for some reason the letter L is added to the end of each index.
My code:
import pandas as 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] for x in indexes]
for 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]]
[[0L, 'Bob', 968], [1L, 'Jessica', 341], [2L, 'Mary', 77], [3L, 'John', 578], [4L, 'Mel', 434]]