Importing data from a MySQL database into Pandas data frame

Importing data from a MySQL database into Pandas data frame

This article illustrates the basic operation of how the dataset imported from the table. The database is taken as MySQL. Yes, It is an essential thing. Without the data, the data analysis and forecasting can’t be done. Right!.

This article illustrates the basic operation of how the dataset imported from the table. The database is taken as MySQL.

Is database is an essential thing for DataScience?

Yes, It is an essential thing. Without the data, the data analysis and forecasting can’t be done. Right!. The data can be any kind of format that may be in CSV, XLS. Consider the retail organization selling their multiple products from the past 5 years. The company decided to forecast the prediction of sales for the next two years.

I grasped what you thinking? Basically, try the data export to CSV file. That can be easily handled in the pandas data frame. But the things not working in such way because prediction does not rely on a single table. It depends on multiple tables say transaction, sales, customer review, the revenue of product from 5 years those need to look in to determine the prediction. The best-opted way will be directly importing the table to the data frame. That will be easier for analysis data against all perspectives.

To connect MySQL using pandas, need to install package ‘mysql-connector-python’ as below command.

pip install mysql-connector-python

For reference

Image for post

Package installation console

import mysql.connector as connection
import pandas as pd

try:
    mydb = connection.connect(host="localhost", database = 'Student',user="root", passwd="root",use_pure=True)
    query = "Select * from studentdetails;"
    result_dataFrame = pd.read_sql(query,mydb)
    mydb.close() #close the connection
except Exception as e:
    mydb.close()
    print(str(e))

mysql pandas-dataframe

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Best MySQL DigitalOcean Performance – ScaleGrid vs. DigitalOcean Managed Databases

Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.

Python Pandas Objects - Pandas Series and Pandas Dataframe

In this post, we will learn about pandas’ data structures/objects. Pandas provide two type of data structures:- ### Pandas Series Pandas Series is a one dimensional indexed data, which can hold datatypes like integer, string, boolean, float...

Basic Dataframe Manipulation using Pandas

Basic Dataframe Manipulation using Pandas. Pandas DataFrames make manipulating your data easy, from selecting or replacing columns and indices to reshaping your data.

How to insert a new row in a Pandas Dataframe? - SaralGyaan

In this Pandas Tutorial, we will learn to insert/add a new row to an existing Pandas Dataframe. We will use pandas.DataFrame.loc, pandas.concat() and numpy.insert(). Using these methods you can add multiple rows/lists to an existing or an empty...

How To Perform Set Operations On Pandas DataFrames

In this article we will explore Pandas Set operations that are Union, Intersection and Difference and will see how to use them?