Đây là một chủ đề cũ hơn, nhưng tôi chỉ muốn kết xuất giải pháp thay thế của mình ở đây. Ban đầu tôi đã thử chunksizetham số (ngay cả với các giá trị khá nhỏ như 10000), nhưng nó không giúp được gì nhiều; vẫn gặp sự cố kỹ thuật với kích thước bộ nhớ (CSV của tôi là ~ 7,5 Gb).
Ngay bây giờ, tôi chỉ đọc các phần của tệp CSV theo cách tiếp cận vòng lặp và thêm chúng vào cơ sở dữ liệu SQLite từng bước một:
import pandas as pd
import sqlite3
from pandas.io import sql
import subprocess
# In and output file paths
in_csv = '../data/my_large.csv'
out_sqlite = '../data/my.sqlite'
table_name = 'my_table' # name for the SQLite database table
chunksize = 100000 # number of lines to process at each iteration
# columns that should be read from the CSV file
columns = ['molecule_id','charge','db','drugsnow','hba','hbd','loc','nrb','smiles']
# Get number of lines in the CSV file
nlines = subprocess.check_output('wc -l %s' % in_csv, shell=True)
nlines = int(nlines.split()[0]) 
# connect to database
cnx = sqlite3.connect(out_sqlite)
# Iteratively read CSV and dump lines into the SQLite table
for i in range(0, nlines, chunksize):
    df = pd.read_csv(in_csv,  
            header=None,  # no header, define column header manually later
            nrows=chunksize, # number of rows to read at each iteration
            skiprows=i)   # skip rows that were already read
    # columns to read        
    df.columns = columns
    sql.to_sql(df, 
                name=table_name, 
                con=cnx, 
                index=False, # don't use CSV file index
                index_label='molecule_id', # use a unique column from DataFrame as index
                if_exists='append') 
cnx.close()