O'Reilly logo

Learning pandas 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 databases that support 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 using the .to_sql() method of DataFrame.

As an example of writing, the following reads the stock data from msft.csv and aapl.csv. It then makes a connection to a SQLite3 database file. If the file does not exist, it creates it on the fly. It then writes the MSFT data to a table named STOCK_DATA. If the table did not exist, it is created. If it exists, all the data is replaced with the MSFT data. It then appends the AAPL stock data to that table:

In [33]:
   # reference SQLite
   import sqlite3

 # read in the ...

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