python - 熊猫读取和大型数据库并在之后将其写回
问题描述
我正在尝试使用 pandas 和 SQL 读取一个大的 .db 文件,但它使用了大量的 ram 并且需要很长时间才能完成。
.db 文件有超过 500 万行。
我怎样才能做同样的事情,但用更少的内存和更快的方式?
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path
#create folder if doesn't exist
NewFolderPath = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
Path(NewFolderPath).mkdir(parents=True, exist_ok=True)
df = pd.DataFrame()
#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
#dest = sqlite3.connect(':memory:')
#conn.backup(dest)
conn.commit()
df = pd.read_sql_query("SELECT * from hist_prices", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices', conn, if_exists='replace', index=False)
解决方案
推荐阅读
- hibernate - JPA Persistence 上下文缓存始终为空并创建新的实例类导致内存泄漏
- macos - Visual Studio for Mac:连接到 TFS
- java - 方法 count 文件中的单词
- html - 制作正确的人字形字体的尖端 - 很棒的图标触摸 div 的结尾
- esp8266 - 写入 Sparkfun ESP8266 Thing 上的串行监视器
- javascript - 如何将此代码转换为 JAVASCRIPT 字符串并稍后对其进行评估?
- dockerfile - 带有 asp 核心控制台应用程序的 Linux Docker 容器
- angular - Angular 6 从材料表中获取排序数据
- dart - 只有在构造函数飞镖中不为空时才更新最终字段
- javascript - 通过 zindex 获取 img src