0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】Pandasのread_sql_queryでMariaDBからテーブルごと直接データフレームに収納するサンプルコード

Last updated at Posted at 2022-01-22

サンプルコード

import numpy as np
import pandas as pd
import pymysql.cursors

connection = pymysql.connect(host='localhost',
                             user='ユーザー名',
                             password='パスワード',
                             db='データベース名',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

sql = "SELECT * from テーブル名;"
df = pd.read_sql_query(sql, connection)

この方法を使うとカラムごとSQLのテーブルを直でDFに収納できる

※なお前提として予めユーザーに該当DBの操作権限が付与されている必要がある

##おまけ:DFを.xlsx化

df = pd.read_sql_query(sql, connection)
df.to_excel("test.xlsx")
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?