O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading and writing from/to SQL databases

Pandas can read data from any SQL database that supports Python data adapters that respect the Python DB-API. Reading is performed using the pandas.io.sql.read_sql() function, and writing to SQL databases is done using the .to_sql() method of DataFrame.

To demonstrate, the following reads the stock data from msft.csv and aapl.csv. It then makes a connection to an SQLite3 database file. If the file does not exist, it is created on the fly. It then writes the MSFT data to a table named STOCK_DATA. If the table does not exist, it is created as well. If it does exist, all the data is replaced with the MSFT data. Finally, it then appends the AAPL stock data to that table:

To demonstrate that this data ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required