简体   繁体   中英

python pandas DataFrame from a list

How do i create a pandas DataFrame from the following list:

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.

You can just pass the list to the DataFrame constructor, and set the index using the column name:

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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