{"id":8133,"date":"2025-05-12T05:17:04","date_gmt":"2025-05-12T05:17:04","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=8133"},"modified":"2025-05-20T07:09:46","modified_gmt":"2025-05-20T07:09:46","slug":"how-to-connect-pandas-to-database","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/","title":{"rendered":"How to Connect Pandas to Database"},"content":{"rendered":"\n<p>Python Pandas is a powerful library that allows you to store, analyze and manipulate data as tables containing rows and columns. It allows you to easily easily aggregate, filter and transform data as per your requirement. Generally, pandas dataframes import data from CSV and TXT files. But sometimes you may need to connect Pandas to relational databases like MySQL, PostgreSQL, Oracle and SQL Server, read data from it or write to the database. In this article, we will learn how to connect Pandas to database.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#Why_Connect_Pandas_to_Database\" >Why Connect Pandas to Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#How_to_Connect_Pandas_to_Database\" >How to Connect Pandas to Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#1_Import_Libraries\" >1. Import Libraries<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#2_Create_Database_Engine\" >2. Create Database Engine<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#3_Read_Data_from_Database_Into_Dataframe\" >3. Read Data from Database Into Dataframe<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#4_Export_Dataframe_to_Database\" >4. Export Dataframe to Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#5_Close_Connection\" >5. Close Connection<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Connect_Pandas_to_Database\"><\/span>Why Connect Pandas to Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You may be wondering why we need to connect Pandas to database when both store data as tables, support indexes, filtering, aggregation and other functions. There are a couple of reasons of this. First of all, depending on the tasks you need to do, sometimes it may be simpler to do it by running an SQL query in database. For example, it may be easier to run SQL join queries than <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-merge-and-join-pandas-dataframes\/\">merging and joining dataframes<\/a>. On the other hand, some tasks may be easily done in Pandas instead of a database. For example, it is a lot easier to <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-pivot-tables-in-python-pandas\/\">create pivot tables in Pandas<\/a>, than using SQL queries.<\/p>\n\n\n\n<p>Secondly, in most cases, the web server and database server are located at different places. When you analyze data using Python pandas, you put your web server to use. When you run SQL query, you use your database server. Depending on web server load, you may want to off load a few database operations to database server and vice versa. This will ensure that neither web server nor database is overloaded.<\/p>\n\n\n\n<p>Thirdly, once you have imported data from database to Pandas, you can easily visualize data by <a href=\"https:\/\/ubiq.co\/mysql-charts\">building charts and reports<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Connect_Pandas_to_Database\"><\/span>How to Connect Pandas to Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are the steps to connect Python Pandas to database, import data from it into the dataframe, and write data from the dataframe back to the database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Import_Libraries\"><\/span>1. Import Libraries<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We will use <a href=\"https:\/\/www.sqlalchemy.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQLAlchemy<\/a> module to connect to database, read\/write to database. It allows you to connect to all major relational databases such as MySQL, PostgreSQL, Oracle and SQL Server. It is not available in Python, by default. So you will need to install it. Open terminal and run the following command to install it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ pip install SQLAlchemy<\/pre>\n\n\n\n<p>Create an empty Python script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo vi test.py<\/pre>\n\n\n\n<p>Add the following lines to it. First, we will import the necessary libraries.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd<br>from sqlalchemy import create_engine<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Create_Database_Engine\"><\/span>2. Create Database Engine<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Next, we will create a database engine. For this purpose, we will use create_engine() function. It will return a connection engine that can be used to query your database. It takes connection string as input. Here is an example of connection to a PostgreSQL\/MySQL database.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">engine = create_engine('postgresql:\/\/username:password@host:port\/database') # for postgresql<br>OR<br>engine = create_engine('mysql:\/\/username:password@host:port\/database')  # for mysql<\/pre>\n\n\n\n<p>In the above command, replace username, password, host, port and database with appropriate values as per your requirement,<\/p>\n\n\n\n<p>You can use <a href=\"https:\/\/www.connectionstrings.com\/\">connectionstrings.com<\/a> to find the connection string for your database. It contains connection string formats for all kinds of databases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Read_Data_from_Database_Into_Dataframe\"><\/span>3. Read Data from Database Into Dataframe<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Once you have established the connection engine, then you can read data from it using read_sql() function. It accepts two arguments &#8211; sql query and connection engine. It allows you to fire SELECT queries to your database. Here is an example to read data from one of the database tables. Replace table_name with the name of the table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sql_query = \"SELECT * FROM <strong>table_name<\/strong>\"<br>df = pd.read_sql(sql_query, engine)<\/pre>\n\n\n\n<p>The result of the above SQL query will be returned as a Pandas dataframe. Thereafter, you can use Pandas functions and operators to work with the result. Here is an example to display first 15 rows in the result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df1 = df[0:15]<br>print(df1)<\/pre>\n\n\n\n<p>Here is an example to read specific columns from your data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df2 = df.loc[:, [col1, col2]].head()<br>print(df2)<\/pre>\n\n\n\n<p>After this step, if your resulting dataframe contains duplicate column or row values, then you can <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-find-duplicates-in-python-pandas-dataframe\/\">find duplicate rows<\/a> using duplicated() function. Alternatively, you can also <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-pandas-dataframe-to-excel-spreadsheets\/\">export Pandas dataframe to Excel spreadsheet<\/a> if you want.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Export_Dataframe_to_Database\"><\/span>4. Export Dataframe to Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also write dataframe data to database using to_sql() function available in Pandas dataframe. Here is an example to demonstrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.to_sql('table_name', engine, if_exists='replace', index=False)<\/pre>\n\n\n\n<p>The to_sql() function accepts the table name to which you want to export data &amp; connection engine. You can also mention optional arguments such as if_exists which specifies what needs to be done if the table already exists. In this case, the table will be replaced if it exists, else it will be newly created. We have also specified here that we do not want any indexing in the database table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Close_Connection\"><\/span>5. Close Connection<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>After you have executed all queries as per your requirement, then it is important to close the database connection to avoid database issues. For this, you can call close() function on the connection object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">engine.close()<\/pre>\n\n\n\n<p>Save and close the file. You can run the script with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo python test.py<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt how to connect to database from Python Pandas, read data from a database into a dataframe using read_sql() function, write data from dataframe to database using to_sql() function. You can modify it as per your requirement. <\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/top-python-libraries-for-machine-learning\/\">Top Python Libraries for Machine Learning<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/machine-learning-life-cycle\/\">Machine Learning Life Cycle<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-machine-learning-works\/\">How Machine Learning Works<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you may need to connect Pandas to database. Here is how to create database connection from Python Pandas.<\/p>\n","protected":false},"author":1,"featured_media":8153,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[421],"class_list":["post-8133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-pandas-databases"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Connect Pandas to Database - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes you may need to connect Pandas to database. Here is how to create database connection from Python Pandas.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Connect Pandas to Database - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes you may need to connect Pandas to database. Here is how to create database connection from Python Pandas.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-12T05:17:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T07:09:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/connect-pandas-to-database.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Connect Pandas to Database\",\"datePublished\":\"2025-05-12T05:17:04+00:00\",\"dateModified\":\"2025-05-20T07:09:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/\"},\"wordCount\":856,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/connect-pandas-to-database.jpg?fit=300%2C200&ssl=1\",\"keywords\":[\"pandas databases\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/\",\"name\":\"How to Connect Pandas to Database - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/connect-pandas-to-database.jpg?fit=300%2C200&ssl=1\",\"datePublished\":\"2025-05-12T05:17:04+00:00\",\"dateModified\":\"2025-05-20T07:09:46+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes you may need to connect Pandas to database. Here is how to create database connection from Python Pandas.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/connect-pandas-to-database.jpg?fit=300%2C200&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/connect-pandas-to-database.jpg?fit=300%2C200&ssl=1\",\"width\":300,\"height\":200,\"caption\":\"connect pandas to database\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-connect-pandas-to-database\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Connect Pandas to Database\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Connect Pandas to Database - Ubiq BI","description":"Sometimes you may need to connect Pandas to database. Here is how to create database connection from Python Pandas.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/","og_locale":"en_US","og_type":"article","og_title":"How to Connect Pandas to Database - Ubiq BI","og_description":"Sometimes you may need to connect Pandas to database. Here is how to create database connection from Python Pandas.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2025-05-12T05:17:04+00:00","article_modified_time":"2025-05-20T07:09:46+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/connect-pandas-to-database.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Connect Pandas to Database","datePublished":"2025-05-12T05:17:04+00:00","dateModified":"2025-05-20T07:09:46+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/"},"wordCount":856,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/connect-pandas-to-database.jpg?fit=300%2C200&ssl=1","keywords":["pandas databases"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/","name":"How to Connect Pandas to Database - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/connect-pandas-to-database.jpg?fit=300%2C200&ssl=1","datePublished":"2025-05-12T05:17:04+00:00","dateModified":"2025-05-20T07:09:46+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes you may need to connect Pandas to database. Here is how to create database connection from Python Pandas.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/connect-pandas-to-database.jpg?fit=300%2C200&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/connect-pandas-to-database.jpg?fit=300%2C200&ssl=1","width":300,"height":200,"caption":"connect pandas to database"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-connect-pandas-to-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Connect Pandas to Database"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/05\/connect-pandas-to-database.jpg?fit=300%2C200&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-27b","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=8133"}],"version-history":[{"count":25,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8133\/revisions"}],"predecessor-version":[{"id":8323,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8133\/revisions\/8323"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/8153"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=8133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=8133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=8133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}