【发布时间】:2016-12-15 04:28:46
【问题描述】:
我的sql.db 文件中有 6 个表。我想知道是否可以创建一个循环来遍历这 6 个表中的每一列,如果单元格值为-,则将值转换为NULL
我目前拥有的代码
for each_item in df.index:
# Note: The name of the tables in the database is the variable 'each_item'
pd.Dataframe.to_sql(df, con=engine, name=each_item, if_exists='append', index=False)
# The below code does not work. And I have no idea why
for each_columns in df.columns:
connection.execute('UPDATE each_item SET each_columns = NULL WHERE each_columns = '-')
这似乎产生了错误。
如果单元格值 = -,我应该如何对其进行编码,以便能够遍历 sql.db 中的所有 tables,并更新 tables 中的每个 column ?
更具体地说,我得到的错误是它无法找到表格。 no such table: each_item
【问题讨论】:
-
tbh id 只是把它放在一个while循环中
-
谢谢,但这对我的问题没有帮助。如果我将表名放在变量中,似乎无法找到
tables -
连接到服务器了吗?
-
你的6张桌子的名字在哪里?您使用的是哪种 SQL DB?
标签: python sql pandas sqlalchemy