pandas to_sql中的dtype引发ValueError

问题描述 投票:0回答:1

我能够使用此代码使用python中的熊猫成功地将CSV数据写入SQLite:

df = pandas.read_csv(path + '/issues.csv')
df.to_sql('issues', connection, if_exists='replace', index=False)

但是,当我尝试使用此代码时,我想根据文档将一列定义为整数:

df = pandas.read_csv(path + '/issues.csv')
df.to_sql('issues', connection, if_exists='replace', index=False, dtype={"severity_int": sqlalchemy.types.Integer()})

我收到一个错误:

File "/usr/local/lib/python3.7/site-packages/pandas/io/sql.py", line 1722, in to_sql
    raise ValueError(f"{col} ({my_type}) not a string")
ValueError: severity_int (INTEGER) not a string

我认为我做对了,但不知道错误在哪里。

python pandas sqlite csv
1个回答
0
投票

我认为您需要将所有列都转换为字符串。试试这个:

df = df.astype(str)
© www.soinside.com 2019 - 2024. All rights reserved.