简体   繁体   English

将 SQLite3 数据库中的所有表合并到一个 Pandas 数据框中

[英]Merge all tables from SQLite3 database into one single pandas dataframe

I would like to undertake some anaylsis of the tables within my database.我想对我的数据库中的表进行一些分析。 But to do this, first I need to merge my sqlite3 tables into one single pandas dataframe.但要做到这一点,首先我需要将我的 sqlite3 表合并到一个 Pandas 数据帧中。

All the tables are identical in structure.所有表的结构都相同。

I would also like the dataframe to have the column headers at the top.我还希望数据框在顶部有列标题。

The link below touched on the subject slightly but doesn't really address my scenario: Merge tables from two different databases - sqlite3/Python下面的链接稍微触及了这个主题,但并没有真正解决我的情况: 合并来自两个不同数据库的表 - sqlite3/Python

Any help to help me merge my sqlite3 tables into one single pandas dataframe would be appreciated任何帮助我将我的 sqlite3 表合并为一个 Pandas 数据框的帮助将不胜感激

I have just stumbled across a simple solution to my own question:我刚刚偶然发现了一个简单的解决方案来解决我自己的问题:

conn = sqlite3.connect('name_of.db')

cur = conn.cursor()


cur.execute('INSERT INTO master_table SELECT * FROM table_name1)
cur.execute('INSERT INTO master_table SELECT * FROM table_name2)
cur.execute('INSERT INTO master_table SELECT * FROM table_name3)
cur.execute('INSERT INTO master_table SELECT * FROM table_name4)
cur.execute('INSERT INTO master_table SELECT * FROM table_name5)

conn.commit()

conn.close()

conn = sqlite3.connect('name_of.db')

df = pd.read_sql_query("SELECT * FROM master_table;", conn) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM