我正在做一個專案,我正在使用psycopg2連接從資料庫中獲取資料,如下所示,
cursor = connection.execute("select * from table")
cursor.fetchall()
現在從表中獲取資料后,我正在運行一些額外的操作來將資料從游標轉換為 pandas 資料框。我正在尋找一些庫或更強大的方法來將資料從 psycopg2 連接轉換為 pandas 資料幀。
任何指導幫助將不勝感激。謝謝
uj5u.com熱心網友回復:
您可以使用 pandas sqlio 模塊在 pandas 資料框中運行和保存查詢。
假設您有 psycopg2 的連接,connection那么您可以像這樣使用 pandas sqlio。
import pandas.io.sql as sqlio
data = sqlio.read_sql_query("SELECT * FROM table", connection)
# Now data is a pandas dataframe having the results of above query.
data.head()
對我來說, sqlio pandas 模塊運行良好。請看一下,如果這就是您要找的,請告訴我。
uj5u.com熱心網友回復:
這可能對您的情況有所幫助:
import pandas.io.sql as sqlio
df = sqlio.read_sql_query(query, connection)
在你的情況下, query = "select * from table"
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/422485.html
標籤:
