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 stock data from CSV msft = pd.read_csv("data/msft.csv") msft["Symbol"]="MSFT" aapl = pd.read_csv("data/aapl.csv") aapl["Symbol"]="AAPL" # create connection connection = sqlite3.connect("data/stocks.sqlite") # .to_sql...