我想使用python的Pandas库读取.xlsx文件,并将数据移植到postgreSQL表.
到目前为止我能做的就是:
import pandas as pd
data = pd.ExcelFile("*File Name*")
Run Code Online (Sandbox Code Playgroud)
现在我知道步骤已成功执行,但我想知道如何解析已读取的excel文件,以便我可以理解excel中的数据如何映射到变量数据中的数据.
我知道如果我没错,数据就是一个Dataframe对象.那么我如何解析这个数据框对象以逐行提取每一行.
尝试使用to_sql将pandas数据帧写入MySQL表.以前一直在使用flavor ='mysql',但是它将来会被折旧并且想要开始转换到使用SQLAlchemy引擎.
示例代码:
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine
engine = create_engine('mysql+mysqlconnector://[user]:[pass]@[host]:[port]/[schema]', echo=False)
cnx = engine.raw_connection()
data = pd.read_sql('SELECT * FROM sample_table', cnx)
data.to_sql(name='sample_table2', con=cnx, if_exists = 'append', index=False)
Run Code Online (Sandbox Code Playgroud)
读取工作正常,但to_sql有错误:
DatabaseError:sql上的执行失败'SELECT name FROM sqlite_master WHERE type ='table'AND name =?;':字符串格式化过程中参数数量错误
为什么看起来它试图使用sqlite?sqlalchemy与mysql,特别是mysql.connector的正确使用是什么?
我也尝试将引擎作为连接传递,这给了我一个引用没有游标对象的错误.
data.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=False)
>>AttributeError: 'Engine' object has no attribute 'cursor'
Run Code Online (Sandbox Code Playgroud) 我需要帮助才能使这个工作.我有一个pd.DataFrame (df)
,我需要加载到MySQL数据库.我不明白错误消息的含义以及如何解决它.
任何帮助将受到高度赞赏.
这是我试过的:
import MySQLdb
from pandas.io import sql
#METHOD 1
db=MySQLdb.connect(host="***",port=***,user="***",passwd="***",db="***")
df.to_sql(con=db, name='forecast', if_exists='replace', flavor='mysql')
##Also tried
sql.write_frame(df, con=db, name='forecast', if_exists='replace', flavor='mysql')
**DatabaseError**: Execution failed on sql: SHOW TABLES LIKE %s
(2006, 'MySQL server has gone away')
unable to rollback
#METHOD 2: using sqlalchemy
from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://**username***:**passwd**@***host***:3306/**dbname**")
conn = engine.raw_connection()
df.to_sql(name='demand_forecast_t', con=conn,if_exists='replace', flavor='mysql',index=False, index_label='rowID')
conn.close()
Run Code Online (Sandbox Code Playgroud)
错误消息是:
**OperationalError**: DatabaseError: Execution failed on sql: SHOW TABLES LIKE %s
(2006, 'MySQL server has gone away') …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Python Pandas Dataframe 写入 MySQL 数据库。我知道这是可能的使用SQLAlchemy为这个,但我不知道是否有另一种方式,可能会更容易,最好是已建成大熊猫。我花了很多时间尝试用 For 循环来做这件事,但这是不现实的。
如果有人知道更好的方法,将不胜感激。
非常感谢!
目前:
问题是MySQLdb或Oursql是必需的,我没有设法让它们中的任何一个工作.
发现这一点,但没有设法让它工作.
编辑:如果您知道其他与Python3兼容的orm,我很感兴趣.
我正在尝试使用以下代码将pandas数据帧写入MySQL数据库.
import pandas as pd
import numpy as np
from pandas.io import sql
import MySQLdb
df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD'), [1.1, 1.7, 2.5, 2.6, 3.3, 3.8,4.0,4.2,4.3,4.5,4.6,4.7,4.7,4.8]]).T
db = MySQLdb.connect("192.168.56.101","nilani","123","test")
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS TEST")
sql = """CREATE TABLE TEST (
ID INT NOT NULL,
COL1 CHAR(20),
COL2 CHAR(20),
COL3 CHAR(20))"""
cursor.execute(sql)
sql.write_frame(df, con=db, name='TEST', flavor='mysql')
db.close()
Run Code Online (Sandbox Code Playgroud)
我一直在提到这个问题和其他资源.我有任何方式得到以下错误.会是什么原因?
sql.write_frame(df, con=db, name='TEST', flavor='mysql')
AttributeError: 'str' object has no attribute 'write_frame'
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个脚本来使用多个 Pandas 数据框填充 mySQL 数据库。为简单起见,我将在此处演示使用单个 Pandas df 填充数据库的代码
我按如下方式连接到数据库:
导入 mysql.connector 导入熊猫作为 pd
# create the cursor and the connector
conn = mysql.connector.connect(
host='localhost',
user='root',
password='my_password')
c = conn.cursor(buffered=True)
# Create the database
c.execute('CREATE DATABASE IF NOT EXISTS ss_json_interop')
# Connect now to the ss_json_interop database
conn = mysql.connector.connect(
host='localhost',
user='root',
password='my_password',
database='ss_json_interop')
c = conn.cursor(buffered=True)
#### Create the table
c.execute("""CREATE TABLE IF NOT EXISTS sample_sheet_stats_json (
ss_ID int NOT NULL AUTO_INCREMENT,
panel text,
run_ID text,
sample_ID text,
i7_index_ID text, …
Run Code Online (Sandbox Code Playgroud)