Interacting with a SQL database through pandas requires the sqlalchemy dependency to be installed.
First, let's define the engine from which connection parameters can be obtained:
engine = create_engine('sqlite:///:memory:')
Now, let's read the data_sql table from the SQL database:
with engine.connect() as conn, conn.begin():
print(pd.read_sql_table('data_sql', conn))
This results in the following output:
![](https://static.packt-cdn.com/products/9781789343236/graphics/assets/e991f948-810a-4d79-9894-cd22d844b593.png)
Output of read_sql_table
The read_sql_table() function reads an entire table for the given table name. A specific column can be set as the index when reading:
pd.read_sql_table('data_sql', engine, index_col='index')
This results in the following output:
![](https://static.packt-cdn.com/products/9781789343236/graphics/assets/357a68c7-d791-454a-8dfe-fdff32a5188c.png)
Output of read_sql_table with indexing
The columns argument lets us choose specific columns when reading data by passing the column names as a list. Any...