How to load data into pandas from a large database?

后端 未结 2 1328
余生分开走
余生分开走 2021-01-14 21:40

I have a postgres database which contains time series data.The size of the database is around 1 GB.Currently to read data, this is what I do

import psycopg2
         


        
2条回答
  •  醉梦人生
    2021-01-14 22:17

    Consider loading this file directly to the database using psql. From your console try this command:

    $ cat file.csv | psql db -c "COPY your_table FROM STDIN DELIMITER ',' CSV HEADER"
    

    Example:

    Consider the following table structure ..

    CREATE TEMPORARY TABLE t (foo INT, bar TEXT);
    

    and the following CSV file ..

    x,y
    1,foo
    2,bar
    3,foo-bar
    

    Execute the following command from your terminal:

    $ cat /tmp/file.csv | psql db -c "COPY t FROM STDIN DELIMITER ',' CSV HEADER"
    

    And here is your data:

    db=# SELECT * FROM t;
     a |    b    
    ---+---------
     1 | foo
     2 | bar
     3 | foo-bar
    (3 Zeilen)
    

提交回复
热议问题