简体   繁体   English

python pandas列表中的DataFrame

[英]python pandas DataFrame from a list

How do i create a pandas DataFrame from the following list: 如何从以下列表创建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']]

and have the first elements '41457', '41458' ... '41489' as the index. 并将第一个元素'41457','41458'...'41489'作为索引。

You can just pass the list to the DataFrame constructor, and set the index using the column name: 您只需将列表传递给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

Note that the columns are just numbers unless you provide column names (note you are specifying the column name, not the index so it's 'a' now): 请注意,除非您提供列名,否则列只是数字(请注意,您指定的是列名,而不是索引,因此它现在是'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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM