LoginSignup
12
7

More than 5 years have passed since last update.

PythonでMySQLに接続してデータをpandasでいじる準備

Last updated at Posted at 2018-06-22

Mysqlに接続してデータをいじる


from urllib.parse import urlparse
import mysql.connector
import pandas as pd
import pandas.io.sql as psql

#         #
# SQL接続 #
#         #
url = urlparse('mysql://root:passwords@127.0.0.1:3306/DB名')
con = mysql.connector.connect(
    host = url.hostname or 'localhost',
    port = url.port or 3306,
    user = url.username or 'root',
    password = url.password or '',
    database = url.path[1:],
)


#                    #
# キーワードを取得・成型 #
#                    #

# SQL読込
read_sql = """SELECT カラム名 FROM テーブル名 where 条件;"""
data = psql.read_sql(read_sql, con)

12
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7