1. 場(chǎng)景描述
Python/ target=_blank class=infotextkey>Python環(huán)境下需要從greenplum/postgresql中,獲取算法執(zhí)行的數(shù)據(jù),但是從數(shù)據(jù)庫(kù)中查詢出來是數(shù)組格式的,算法無法使用,需要轉(zhuǎn)換為dataframe格式。
2. 解決方案
結(jié)合第三方pandas使用
2.1 數(shù)據(jù)庫(kù)調(diào)用類
import dbgp as dbgp
data = dbgp.queryGp(sql)
2.2 數(shù)據(jù)庫(kù)類
## 導(dǎo)入psycopg2包
import pandas as pd
import psycopg2def queryGp(sql): ## 連接到一個(gè)給定的數(shù)據(jù)庫(kù) conn = psycopg2.connect(dbname="db_laowang", user="laowang", password="123",
port="5432", host="10.192.168.11", client_encoding="UTF-8")
df = pd.read_sql(sql, con=conn) ## 關(guān)閉數(shù)據(jù)庫(kù)連接 conn.close() return df
重點(diǎn)就這一行:df = pd.read_sql(sql, con=conn)
END/歷史推薦:
1. 微服務(wù)時(shí)代之注冊(cè)中心及網(wǎng)關(guān)選型與完整部署
2. 阿里云之?dāng)?shù)據(jù)中臺(tái)
3. springcloud-路由網(wǎng)關(guān)Zuul
4. 軟件開發(fā)之前后端分離Swagger2
5. 數(shù)據(jù)庫(kù)架構(gòu)介紹
6. rabbitmq部署及使用
I’m 「軟件老王」,IT技術(shù)及相關(guān)干貨分享,喜歡我的文章歡迎關(guān)注及轉(zhuǎn)發(fā)!






