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 was created, you can open the database file with a tool such as SQLite Data Browser (available at https://github.com/sqlitebrowser/sqlitebrowser). The following screenshot shows you a few rows of the data in the database file:

Data can be read using SQL from...