📊 Unit 1 – DataFrame · Lesson 15
MySQL के साथ Pandas
pandas और MySQL के बीच data क्यों?
संस्थाएँ data को database (MySQL) में सुरक्षित रखती हैं पर उसका analysis pandas से करती हैं। pandas किसी table या query के result को सीधे DataFrame में पढ़ सकता है, और DataFrame को वापस table के रूप में save कर सकता है।
Connectors install करना
Command prompt
pip install pymysql sqlalchemy
नीचे के programs में अपना MySQL user name और password लिखें।
Database बनाना
Python
import pymysql
con = pymysql.connect(host='localhost', user='root', password='root123')
cur = con.cursor()
cur.execute('CREATE DATABASE IF NOT EXISTS shop')
print('Database ready')
con.close()Database ready
Engine
Python
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:root123@localhost/shop')
# driver user password host databaseExport: to_sql()
Python
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:root123@localhost/shop')
stock = pd.DataFrame({'Item': ['Pen', 'Pencil', 'Notebook', 'Eraser', 'Ruler'],
'Category': ['Writing', 'Writing', 'Paper', 'Other', 'Other'],
'Qty': [120, 300, 80, 150, 60],
'Price': [10, 5, 45, 3, 12]})
stock.to_sql('stock', engine, if_exists='replace', index=False)
print('Rows written:', len(stock))Rows written: 5
Import: read_sql()
Python
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:root123@localhost/shop')
df = pd.read_sql('SELECT * FROM stock', engine)
print(df)
print(df.dtypes) Item Category Qty Price
0 Pen Writing 120 10
1 Pencil Writing 300 5
2 Notebook Paper 80 45
3 Eraser Other 150 3
4 Ruler Other 60 12
Item object
Category object
Qty int64
Price int64
dtype: object
Python
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:root123@localhost/shop')
q = 'SELECT Item, Qty * Price AS Value FROM stock WHERE Qty >= 100 ORDER BY Value DESC'
print(pd.read_sql(q, engine)) Item Value
0 Pencil 1500
1 Pen 1200
2 Eraser 450
Rows जोड़ना
Python
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:root123@localhost/shop')
new = pd.DataFrame({'Item': ['Sharpener'], 'Category': ['Other'], 'Qty': [90], 'Price': [6]})
new.to_sql('stock', engine, if_exists='append', index=False) # add rows
df = pd.read_sql('SELECT * FROM stock', engine)
print(df.groupby('Category')['Qty'].sum())Category
Other 300
Paper 80
Writing 420
Name: Qty, dtype: int64
CSV → MySQL
Python
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:root123@localhost/shop')
df = pd.DataFrame({'Item': ['Glue', 'Marker'], 'Category': ['Other', 'Writing'],
'Qty': [40, 75], 'Price': [20, 25]})
df.to_csv('stock_new.csv', index=False) # a CSV file, e.g. saved from Excel
pd.read_csv('stock_new.csv').to_sql('stock', engine, if_exists='append', index=False)
print(pd.read_sql('SELECT COUNT(*) AS items FROM stock', engine)) items
0 8
Parameters का सारांश
| Call | Meaning |
|---|---|
| create_engine(url) | Connection to the database |
| df.to_sql(name, engine, if_exists, index) | Write DataFrame to table name |
| pd.read_sql(query_or_table, engine) | Run a SELECT and get a DataFrame |
| if_exists='replace' / 'append' / 'fail' | What to do when the table exists |
| index=False | Do not store the DataFrame index |
अक्सर पूछे जाने वाले प्रश्न (FAQ)
to_sql() में if_exists क्या करता है?
'fail' (default) table हो तो error देता है, 'replace' उसे हटाकर फिर बनाता है, और 'append' मौजूदा table में rows जोड़ता है।
pandas को MySQL से जोड़ने के लिए कौन सी libraries चाहिए?
pymysql (MySQL driver) और sqlalchemy (engine बनाने के लिए)। दोनों pip से install करें।
Connection string का format क्या है?
'mysql+pymysql://user:password@host/database', जैसे 'mysql+pymysql://root:root123@localhost/shop'।
