导入所需要的库
# pip install pandas -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
# pip install pandasql -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
# pip install sqlalchemy==1.4.46 -i https://pypi.doubanio.com/simple --trusted-host pypi.douban.com
import pandas as pd
from pandasql import sqldf
# 读取 Excel 文件, /文件名 第一张表
df = pd.read_excel(r'证照数据库.xlsx', sheet_name=0)
# 执行 SQL 查询, sql语句 , globals()
result = sqldf("SELECT * FROM df where 片区='市片区'")
# 打印查询结果
print(result)
第二种存入内存
import sqlite3
import pandas as pd
# 读取Excel文件
df = pd.read_excel(r'证照数据库.xlsx', sheet_name=0)
# 创建内存数据库连接
conn = sqlite3.connect(':memory:')
# 将DataFrame写入内存数据库
df.to_sql('data_table', conn, index=False)
# 执行SQL查询
result = pd.read_sql_query("SELECT * FROM data_table WHERE 名称='小王'", conn)
print(result)
# 关闭连接,自动清空
conn.close()

被折叠的 条评论
为什么被折叠?



