Skip to content

Instantly share code, notes, and snippets.

@FlameWert
Last active February 3, 2020 23:53
Show Gist options
  • Save FlameWert/60107b00825733c9e55b7ba44937ec67 to your computer and use it in GitHub Desktop.
Save FlameWert/60107b00825733c9e55b7ba44937ec67 to your computer and use it in GitHub Desktop.
Convert the result of SQL Query into Pandas DataFrame
import pyodbc
import pandas as pd
connection_string = "Driver={SQL Server};Server=localhost;Database=LionKing;Trusted_Connection=yes"
connection = pyodbc.connect(connection_string)
# Remember it is connection and not connection or Connection
# The function name is connection which will return a data type of Connection
query = "SELECT * FROM dbo.Animals"
df = pd.read_sql(query, connection) # Query first and then connection
df.head()
@FlameWert
Copy link
Author

A small piece of code to convert result of SQL Query into Pandas DataFrame

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment