您的位置:首页 > 游戏 > 手游 > 房产信息网海南_用自己的名字设计头像_百度搜索下载_平面设计培训

房产信息网海南_用自己的名字设计头像_百度搜索下载_平面设计培训

2025/4/23 6:56:41 来源:https://blog.csdn.net/eqwaak0/article/details/147313998  浏览:    关键词:房产信息网海南_用自己的名字设计头像_百度搜索下载_平面设计培训
房产信息网海南_用自己的名字设计头像_百度搜索下载_平面设计培训

一、环境准备与数据库连接

1.1 安装依赖库

pip install pandas sqlalchemy psycopg2  # PostgreSQL
# 或
pip install pandas sqlalchemy pymysql  # MySQL
# 或
pip install pandas sqlalchemy          # SQLite

1.2 创建数据库引擎

通过SQLAlchemy创建统一接口:

from sqlalchemy import create_engine# PostgreSQL示例
engine = create_engine('postgresql+psycopg2://user:password@host:port/dbname')# MySQL示例 
engine = create_engine('mysql+pymysql://user:password@host:port/dbname')# SQLite示例
engine = create_engine('sqlite:///mydatabase.db')

二、数据库读取操作

2.1 读取整张表

import pandas as pd# 读取users表全部数据
df = pd.read_sql('users', con=engine)
print(df.head())

2.2 执行复杂查询

query = """
SELECT user_id, COUNT(order_id) AS order_count,SUM(amount) AS total_spent
FROM orders
WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31'
GROUP BY user_id
HAVING total_spent > 1000
"""result_df = pd.read_sql(query, con=engine)

2.3 分块读取大数据集

chunk_size = 10000
chunks = pd.read_sql('large_table', con=engine, chunksize=chunk_size)for chunk in chunks:process_chunk(chunk)  # 自定义处理函数

三、数据写入数据库

3.1 整表写入

# 将DataFrame写入新表
df.to_sql('new_table', con=engine, if_exists='replace',  # 存在则替换index=False
)

3.2 追加写入模式

# 追加数据到现有表
df.to_sql('existing_table',con=engine,if_exists='append',index=False
)

3.3 批量写入优化

# 使用method='multi'加速写入
df.to_sql('high_perf_table',con=engine,if_exists='append',index=False,method='multi', chunksize=1000
)

四、高级技巧与性能优化

4.1 数据类型映射

自定义类型转换保证数据一致性:

Pandas类型SQL类型(PostgreSQL)处理方案
objectVARCHAR自动转换
int64BIGINT检查数值范围
datetime64TIMESTAMP指定dtype参数
categoryENUM手动创建ENUM类型
from sqlalchemy.dialects.postgresql import VARCHAR, INTEGERdtype = {'user_name': VARCHAR(50),'age': INTEGER
}df.to_sql('users', engine, dtype=dtype, index=False)

4.2 事务管理

from sqlalchemy import textwith engine.begin() as conn:# 删除旧数据conn.execute(text("DELETE FROM temp_table WHERE create_date < '2023-01-01'"))# 写入新数据df.to_sql('temp_table', con=conn, if_exists='append', index=False)

4.3 并行处理加速

from concurrent.futures import ThreadPoolExecutordef write_chunk(chunk):chunk.to_sql('parallel_table', engine, if_exists='append', index=False)with ThreadPoolExecutor(max_workers=4) as executor:chunks = np.array_split(df, 8)executor.map(write_chunk, chunks)

五、常见问题解决方案

5.1 编码问题处理

# 指定连接编码
engine = create_engine('mysql+pymysql://user:pass@host/db',connect_args={'charset': 'utf8mb4'}
)

5.2 日期时间处理

# 读取时转换时区
df = pd.read_sql('SELECT * FROM events',con=engine,parse_dates={'event_time': {'utc': True}}
)# 写入时指定时区
from sqlalchemy import DateTime
dtype = {'event_time': DateTime(timezone=True)}

5.3 内存优化

# 指定低精度类型
dtype = {'price': sqlalchemy.Numeric(10,2),'quantity': sqlalchemy.SmallInteger
}df.to_sql('products', engine, dtype=dtype)

六、完整工作流示例

mermaid:

graph LR
A[数据库连接] --> B[执行SQL查询]
B --> C[获取DataFrame]
C --> D[数据清洗转换]
D --> E[分析处理]
E --> F[结果写入数据库]

七、性能对比测试

数据规模直接写入(秒)批量写入(秒)提升比例
10万条45.212.372.8%
100万条432.189.779.2%
1000万条内存溢出256.4-

八、最佳实践总结

  1. 连接管理:始终使用上下文管理器确保连接关闭

  2. 类型声明:显式定义字段类型避免隐式转换

  3. 批量操作:合理设置chunksize提升吞吐量

  4. 索引优化:为查询字段添加数据库索引

  5. 错误处理:添加重试机制应对网络波动

完整示例代码仓库:GitHub链接
扩展阅读:《Pandas高效数据处理技巧》


通过掌握这些核心技巧,您可以将Pandas的灵活数据处理能力与数据库的强大存储管理完美结合,构建高效可靠的数据流水线。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com