Importing data into pandas from a database

The pandas library also has functions to support the import of tables directly from SQL databases. Functions that can accomplish this include read_sql_query() and read_sql_table(). Before using these functions, the connection to the database must be established so that it can be passed to the function. In the following example, a table from a SQLite database is read into a DataFrame using the read_sql_query() function:

import sqlite3

conn = sqlite3.connect(pt_db_full_path)
table_name = 'TABLE1'
pt_data = pd.read_sql_query('SELECT * from ' + table_name + ';',conn)

If you wish to connect to a standard database, such as a MySQL database, the code would be similar, except for the connection statement, which would use the corresponding function for a MySQL database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset