How to Convert SQL Query Results to Pandas Dataframe Using pypyodbc? Last Updated : 22 Nov, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we are going to see how to convert SQL Query results to a Pandas Dataframe using pypyodbc module in Python. We may need database results from the table using different queries to work on the data and apply any machine learning on the data to analyze the things and the suggestions better. We can convert our data into python Pandas dataframe to apply different machine algorithms to the data. Let us see how we can the SQL query results to the Pandas Dataframe using MS SQL as the server. pypyodbc: It is a pure Python Cross-Platform ODBC interface module. To Install pypyodbc module to access the ODBC databases using this command in the terminal. pip install pypyodbcCreating DatabaseStep 1: Create a DatabaseCREATE DATABASE GeeksforGeeks;Step 2: Using the databaseUSE GeeksForGeeksStep 3: Creating table student_marks and adding rows into the tableCREATE TABLE student_marks( stu_id VARCHAR(20), stu_name VARCHAR(20), stu_branch VARCHAR(20), total_marks INT )Converting SQL Query to Pandas Dataframe Example 1: Connect to the MSSQL server by using the server name and database name using pdb.connect(). And then read SQL query using read_sql() into the pandas data frame and print the data. Python3 import pypyodbc as pdb import pandas as pd connection = pdb.connect(""" Driver={{SQL Server Native Client 11.0}}; Server={0}; Database={1}; Trusted_Connection=yes;""".format('LAPTOP-LKHL8PKV', 'GeeksForGeeks') ) query = """SELECT * FROM student_marks""" table = pd.read_sql(query, connection) print(table) Output: Example 2: Query to get students of E.C.E branch from the table to the pandas data frame. Python3 import pypyodbc as pdb import pandas as pd connection = pdb.connect(""" Driver={{SQL Server Native Client 11.0}}; Server={0}; Database={1}; Trusted_Connection=yes;""".format('LAPTOP-LKHL8PKV', 'GeeksForGeeks') ) query = """SELECT * FROM student_marks WHERE stu_branch='E.C.E'""" table = pd.read_sql(query, connection) print(table) Output: Comment More infoAdvertise with us Next Article How To Connect and run SQL Queries to a PostgreSQL Database from Python L lokeshpotta20 Follow Improve Article Tags : Pandas python-modules Python-pandas Similar Reads How to Convert Pandas to PySpark DataFrame ? In this article, we will learn How to Convert Pandas to PySpark DataFrame. Sometimes we will get csv, xlsx, etc. format data, and we have to store it in PySpark DataFrame and that can be done by loading data in Pandas then converted PySpark DataFrame. For conversion, we pass the Pandas dataframe int 3 min read How to convert pandas DataFrame into SQL in Python? In this article, we aim to convert the data frame into an SQL database and then try to read the content from the SQL database using SQL queries or through a table. Convert Pandas DataFrame into SQL in PythonBelow are some steps by which we can export Python dataframe to SQL file in Python: Step 1: I 4 min read Converting Django QuerySet to Pandas DataFrame Django's ORM provides a powerful way to query databases and retrieve data using QuerySet objects. However, there are times when you may need to manipulate, analyze, or visualize this data in a more sophisticated way than what Django alone can offer. In such cases, pandas, a popular data manipulation 5 min read How to write Pandas DataFrame to PostgreSQL table? In this article, we will be looking at some methods to write Pandas dataframes to PostgreSQL tables in the Python. Method 1: Using to_sql() function to_sql function is used to write the given dataframe to a SQL database. Syntax df.to_sql('data', con=conn, if_exists='replace', index=False) Parameter 3 min read How To Connect and run SQL Queries to a PostgreSQL Database from Python In this PostgreSQL Python tutorial, we will explain how to connect to a PostgreSQL database using Python and execute SQL queries. Using the powerful psycopg2 library, we can seamlessly interact with our PostgreSQL database from Python, making it easy to perform tasks like inserting, updating, and re 4 min read Create a SQL table from Pandas dataframe using SQLAlchemy In this article, we will discuss how to create a SQL table from Pandas dataframe using SQLAlchemy. As the first steps establish a connection with your existing database, using the create_engine() function of SQLAlchemy. Syntax: from sqlalchemy import create_engine engine = create_engine(dialect+driv 3 min read Like