4

如何从以下列表中创建 pandas DataFrame:

ls= \
[['41457', 'PRE', '533', '887', '1/3/1978', '2/1/2008', '[REL]', '217', '197800000003'],\
['41458', 'PRE', '533', '887', '1/3/1978', '2/1/2008', '[REL]', '217', '197800000004'],\
['41460', 'PRE', '780', '887', '1/3/1978', '2/1/2008', '[D/T]', '217', '197800000006'],\
['41461', 'PRE', '780', '887', '1/3/1978', '2/1/2008', '[D/T]', '217', '197800000007'],\
['41462', 'PRE', '645', '887', '1/3/1978', '2/1/2008', '[AGREE]', '217', '197800000008'],\
['41463', 'PRE', '645', '887', '1/3/1978', '2/1/2008', '[AGREE]', '217', '197800000009'],\
['41464', 'PRE', '645', '887', '1/3/1978', '2/1/2008', '[AGREE]', '217', '197800000010'],\
['41465', 'PRE', '645', '887', '1/3/1978', '2/1/2008', '[AGREE]', '217', '197800000011'],\
['41489', 'PRE', '533', '887', '1/3/1978', '2/1/2008', '[REL]', '492', '197800000035']]

并将第一个元素 '41457', '41458' ... '41489' 作为索引。

4

1 回答 1

3

您可以将列表传递给DataFrame构造函数,并使用列名设置索引:

In [5]: df = pd.DataFrame(ls).set_index(0)

In [6]: df
Out[6]: 
         1    2    3         4         5        6    7             8
0                                                                   
41457  PRE  533  887  1/3/1978  2/1/2008    [REL]  217  197800000003
41458  PRE  533  887  1/3/1978  2/1/2008    [REL]  217  197800000004
41460  PRE  780  887  1/3/1978  2/1/2008    [D/T]  217  197800000006
41461  PRE  780  887  1/3/1978  2/1/2008    [D/T]  217  197800000007
41462  PRE  645  887  1/3/1978  2/1/2008  [AGREE]  217  197800000008
41463  PRE  645  887  1/3/1978  2/1/2008  [AGREE]  217  197800000009
41464  PRE  645  887  1/3/1978  2/1/2008  [AGREE]  217  197800000010
41465  PRE  645  887  1/3/1978  2/1/2008  [AGREE]  217  197800000011
41489  PRE  533  887  1/3/1978  2/1/2008    [REL]  492  197800000035

请注意,除非您提供列名,否则列只是数字(请注意,您指定的是列名,而不是索引,因此现在是“a”):

In [7]: df = pd.DataFrame(ls, columns=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']).set_index('a')

In [8]: df
Out[8]: 
         b    c    d         e         f        g    h             i
a                                                                   
41457  PRE  533  887  1/3/1978  2/1/2008    [REL]  217  197800000003
41458  PRE  533  887  1/3/1978  2/1/2008    [REL]  217  197800000004
41460  PRE  780  887  1/3/1978  2/1/2008    [D/T]  217  197800000006
41461  PRE  780  887  1/3/1978  2/1/2008    [D/T]  217  197800000007
41462  PRE  645  887  1/3/1978  2/1/2008  [AGREE]  217  197800000008
41463  PRE  645  887  1/3/1978  2/1/2008  [AGREE]  217  197800000009
41464  PRE  645  887  1/3/1978  2/1/2008  [AGREE]  217  197800000010
41465  PRE  645  887  1/3/1978  2/1/2008  [AGREE]  217  197800000011
41489  PRE  533  887  1/3/1978  2/1/2008    [REL]  492  197800000035
于 2013-06-18T14:39:28.280 回答