简体   繁体   English

如何将 Pandas 数据框转换为 sqlite 数据库

[英]How to convert pandas dataframe to sqlite database

I have a single database .pd which I want to disaggregate into several tables to form a single sql database .db我有一个单一的数据库 .pd,我想将它分解成几个表以形成一个单一的 sql 数据库 .db

I have previously created the empty .db我之前创建了空的 .db

data_path = r'C:\Users\User\Desktop\homeworkpython\db.db'

conn = sql.connect(data_path)
cursor = conn.cursor()

The mother base is 'df'.母基是'df'。

so, df.head(1) show所以, df.head(1)显示

coordinates     created_at     extended_entities     geo      id
  None          Sat Dec 14           NaN            None      1

I create my first table我创建了我的第一个表

df1 = df[['created_at ','id']]
from sqlalchemy import create_engine
engine = create_engine(r'sqlite:///C:\Users\User\Desktop\homeworkpython\db.db')

df1.to_sql('tab1', engine)

But i have doubts about whether the table was well created and is inside the .db base, how to know?但是我怀疑该表是否创建良好并且是否在 .db 库中,如何知道?

option 1选项1

read table data into dataframe, you may use these link将表数据读入数据帧,您可以使用这些链接

df2 = pd. read_sql('select * from tab1', engine)
print(df2)

option 2选项 2

You can install any sqlite database explorer like one in these link您可以在这些链接中安装任何 sqlite 数据库资源管理器

Using these tool, you can explore the database使用这些工具,您可以探索数据库

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

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