简体   繁体   English

将pandas整数数据帧保存到csv(缺少值)以导入到SQL Server

[英]Save pandas integer dataframe to csv (with missing values) for import into SQL Server

I have scraped some baseball data from the web using Python and have saved it in a Pandas dataframe. 我使用Python从网络上抓取了一些棒球数据,并将其保存在Pandas数据框中。 One of the columns contains the numeric player identifying code, but it often null. 列之一包含数字播放器标识代码,但通常为null。 Because Pandas integer columns cannot contain NaNs, this column has been converted to float and saved as such when I export to the data to csv. 由于Pandas整数列不能包含NaN,因此当我将数据导出到csv时,此列已转换为float并保存为此类。

This causes problems when I try and run a bulk insert into the MS SQL Server database as the relevant column is defined to be of type 'INT' (and also contains a foreign key constraint to another table). 当我尝试将批量插入运行到MS SQL Server数据库中时,这会导致问题,因为相关列被定义为“ INT”类型(并且还包含另一个表的外键约束)。 The following error message is raised: 引发以下错误消息:

Bulk load data conversion error (type mismatch or invalid character for the specified codepage) 批量加载数据转换错误(类型不匹配或指定代码页的字符无效)

I tried the suggestion provided here , but it seems changing the format of the float does not solve the problem and SQL Server still throws the same error. 我尝试了此处提供的建议,但似乎更改float的格式不能解决问题,并且SQL Server仍会引发相同的错误。

I am aware that packages such as pyodbc provide an alternate way of transferring data to a database, but my dataframe is quite large (approx 6M rows, 70 cols) and I have found the process too slow, at least compared to saving to csv and importing. 我知道像pyodbc这样的软件包提供了将数据传输到数据库的另一种方法,但是我的数据帧很大(大约6M行,70列),我发现该过程太慢了,至少与保存到csv和输入。

How can I best solve this problem? 我怎样才能最好地解决这个问题? Ultimately, I'm not wedded to saving to csv and using bulk insert in SQL Server, but I do want a method that is similarly fast. 最终,我不愿意保存到csv并在SQL Server中使用批量插入,但是我确实希望有一种速度同样快的方法。

pd.fillna(-1)

This will fill all blank values to a desired value (such as a negative value). 这会将所有空白值填充为所需的值(例如负值)。 See the documentation for more parameters. 请参阅文档以获取更多参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM