如何将pandas df中的多个列组合成一个列表

时间:2018-04-23 02:33:29

标签: python list pandas

如何将数据框中的多个列合并到一个列表中?

输入:

df = pd.DataFrame(np.random.randn(10000, 7), columns=list('ABCDEFG'))

如果我想从A列创建一个列表,我会执行:

df1 = df['A'].tolist()

但是,如果我想将多个列组合到此列表中,则写df['A','B','C'...'Z'].tolist()

效率不高

我尝试过以下操作,但只是将列标题添加到列表中。

df1 = list(df.columns)[0:8]

预期输入:

   A         B         C         D         E         F         G  
0  0.787576  0.646178 -0.561192 -0.910522  0.647124 -1.388992  0.728360   
1  0.265409 -1.919283 -0.419196 -1.443241 -2.833812 -1.066249  0.553379   
2  0.343384  0.659273 -0.759768  0.355124 -1.974534  0.399317 -0.200278

预期输出:

[0.787576,  0.646178, -0.561192, -0.910522,  0.647124, -1.388992,  0.728360,   
 0.265409, -1.919283, -0.419196, -1.443241, -2.833812, -1.066249,  0.553379,   
 0.343384,  0.659273, -0.759768,  0.355124, -1.974534,  0.399317, -0.200278]

2 个答案:

答案 0 :(得分:0)

您可以使用to_dict

df = pd.DataFrame(np.random.randn(10, 10), columns=list('ABCDEFGHIJ'))
df.to_dict('l')
Out[1036]: 
{'A': [-0.5611441440595607,
  -0.3785906500723589,
  -0.19480328695097676,
  -0.7472526275034221,
  -2.4232786057647457,
  0.10506614562827334,
  0.4968179288412277,
  1.635737019365132,
  -1.4286421753281746,
  0.4973223222844811],
 'B': [-1.0550082961139444,
  -0.1420067090193365,
  0.30130476834580633,
  1.1271866812852227,
  0.38587456174846285,
  -0.531163142682951,
  -1.1335754634118729,
  0.5975963084356348,
  -0.7361022807495443,
  1.4329395663140427],


 ...}

或添加values.tolist()

df[list('ABC')].values.tolist()
Out[1041]: 
[[0.09552771302434987, 0.18551596484768904, -0.5902249875268607],
 [-1.5285190712746388, 1.2922627021799646, -0.8347422966138306],
 [-0.4092028716404067, -0.5669107267579823, 0.3627970727410332],
 [-1.3546346273319263, -0.9352316948439341, 1.3568726575880614],
 [-1.3509518030469496, 0.10487182694997808, -0.6902134363370515]]

修改:np.concatenate(df[list('ABC')].T.values.tolist())

答案 1 :(得分:0)

这就是你要找的东西

lst = df.values.tolist()
flat_list = [item for x in lst for item in x]
print(flat_list)