将一些列从pandas dataframe转换为列表列表

时间:2015-07-07 13:56:51

标签: python python-2.7 pandas

我有dataframe。我想将一些数据转换为列表列表。我感兴趣的列是indexNameBirths。我的代码有效,但似乎效率低下,并且由于某种原因,字母L被添加到每个索引的末尾。

我的代码:

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

期望的输出:

[[0, 'Bob', 968], [1, 'Jessica', 341], [2, 'Mary', 77], [3, 'John', 578], [4, 'Mel', 434]]

2 个答案:

答案 0 :(得分:4)

为什么不按照您的提及使用.values.tolist()

import pandas as pd

# your 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]]

答案 1 :(得分:1)

好的,这有效(基于李健勋的回答和评论):

import pandas as 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)

# Output
print df.reset_index()[['index', 'Names', 'Births']].values.astype(str).tolist()

谢谢李健勋,这也帮助了我: - )

通常,可以使用以下内容将完整的数据框转换为列表列表(这是我需要的):

df.values.astype(str).tolist()