我有一个sqlite3数据库列表,希望能够通过循环将每个数据库读取到单独的Pandas Dataframe中,但到目前为止,我的方法尚未填充该数据帧。我想这与处理副本而不是参考有关,但是找不到一种可行的方法。
import sqlite3
import pandas as pd
db_1 = 'db1.db'
db_2 = 'db2.db'
df_1 = pd.DataFrame()
df_2 = pd.DataFrame()
db_df_name = [(db_1, df_1, 'df_raw'), (db_2, df_2, 'df_processed')]
for (db, df, name) in db_df_name:
with sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES) as connection:
df = pd.read_sql_query("select * from messages;", connection)
print(f'Database {name} processed')
答案 0 :(得分:0)
如果您可以更好地解释您想做什么,我可以提供更多帮助。
尝试一下,如果不起作用请纠正我:
import sqlite3
import pandas as pd
# Assuming that you have two databases...
# which i don't understand cause you use the
# same query below with the same table.
dbs = ['db1.db','db2.db']
db_df_name = [(dbs[0], 'df_raw'), (dbs[1],'df_processed')]
df_1 = pd.DataFrame()
df_2 = pd.DataFrame()
df_list = []
for db, name in db_df_name:
with sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES) as connection:
df = pd.read_sql_query("select * from messages;", connection)
print(f'Database {name} processed')
df_list = df_list.append(df.copy())
df_1, df_2 = df_list
print(df_1)
print(df_2)
使用此命令时:for db, name in db_df_name
, db 是作用域变量,它本身不是 df_1 ,假设您正在传递此结构(这就是我删除):
db_df_name = [(db_1, df_1, 'df_raw'), (db_2, df_2, 'df_processed')]
因此,当您分配df = ...
时, df_1 将为空。