How to connect with a database via Python Pandas

How to connect with a database via Python Pandas

The requirement

Then go to your commandline and type in

  • Install python on your pc
  • pip3 install mysql-connector-python
  • pip3 install matplotlib
  • pip3 install pandas
  • getpass is an in-built tool in python that means you don’t need to install it.

The code

import mysql.connector
import pandas as pd
import matplotlib.pyplot as plt
from getpass import getpass

input = getpass()


db = mysql.connector.connect(
        host="localhost",
        user="sven",
        password=input,
        database="Wahlen_2025"
        )

query = "SELECT Jahr,CDU,SPD,AfD,Grüne,FDP,Linke FROM Essen_NRW"

df = pd.read_sql(query, db)
df.plot()
plt.show()

db.close()

print(df)

Wahlen = elections.

It is a database about the elections results in my home town here in Essen until 2005.

The video to the script

Here is my video (13:57 minutes) on my YouTube channel about it,

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

seventy seven + = eighty seven