我有一个比较大的csv文件,需要将其加载到Postgres中。我正在用熊猫来做到这一点。
我的csv文件大约有500,000行30列。将文件加载到数据帧中没问题,但是将其导入Postgres非常慢。目前,我正在使用此代码执行此操作:
df = pd.read_csv(
'/home/data/data.txt',
sep='|',
dtype=object,
)
print('loaded data')
df.to_sql(
name='table',
con=engine,
schema='public',
method='multi',
index=False,
if_exists='replace',
chunksize=10_000
)
print('imported {} rows'.format(len(df)))