Last active
February 3, 2020 23:53
-
-
Save FlameWert/60107b00825733c9e55b7ba44937ec67 to your computer and use it in GitHub Desktop.
Convert the result of SQL Query into Pandas DataFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A small piece of code to convert result of SQL Query into Pandas DataFrame