python pandas写入mysql,Python Pandas使用NaN值写入sql

I'm trying to read a few hundred tables from ascii and then write them to mySQL. It seems easy to do with Pandas but I hit an error that doesn't make sense to me:

I have a data frame of 8 columns. Here is the column list/index:

metricDF.columns

Index([u'FID', u'TYPE', u'CO', u'CITY', u'LINENO', u'SUBLINE', u'VALUE_010', u'VALUE2_015'], dtype=object)

I then use to_sql to append the data up to mySQL

metricDF.to_sql(con=con, name=seqFile, if_exists='append', flavor='mysql')

I get a strange error about a column being "nan":

OperationalError: (1054, "Unknown column 'nan' in 'field list'")

As you can see all my columns have names. I realize mysql/sql support for writing appears in development so perhaps that's the reason? If so is there a work around? Any suggestions would be greatly appreciated.

解决方案

Update: starting with pandas 0.15, to_sql supports writing NaN values (they will be written as NULL in the database), so the workaround described below should not be needed anymore (see https://github.com/pydata/pandas/pull/8208).

Pandas 0.15 will be released in coming October, and the feature is merged in the development version.

This is probably due to NaN values in your table, and this is a known shortcoming at the moment that the pandas sql functions don't handle NaNs well (https://github.com/pydata/pandas/issues/2754, https://github.com/pydata/pandas/issues/4199)

As a workaround at this moment (for pandas versions 0.14.1 and lower), you can manually convert the nan values to None with:

df2 = df.astype(object).where(pd.notnull(df), None)

and then write the dataframe to sql. This however converts all columns to object dtype. Because of this, you have to create the database table based on the original dataframe. Eg if your first row does not contain NaNs:

df[:1].to_sql('table_name', con)

df2[1:].to_sql('table_name', con, if_exists='append')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题: python pandas写入csv文件的方法有哪些? 回答: 在使用PythonPandas库进行数据处理时,可以使用df.to_csv()方法将数据写入csv文件。该方法有多个参数可以进行设置,比如文件存储路径、分隔符、缺失填充等。\[1\]例如,可以使用以下代码将DataFrame写入csv文件: ```python import pandas as pd data_list = \[\['name','age','job'\],\['Lucy',33,'doctor'\],\['Tom',34,'teacher'\],\['Anna',22,'student'\]\] df = pd.DataFrame(data_list\[1:\],columns=data_list\[0\]) df.to_csv('data.csv', index=False, sep=',') ``` 这样就会将DataFrame写入名为"data.csv"的csv文件中,使用逗号作为分隔符,并且不包含行索引。\[2\]另外,Pandas还支持自动推断压缩类型,可以直接将DataFrame写入压缩文件中,例如: ```python df.to_csv('data.csv.zip') ``` 这样就会将DataFrame写入名为"data.csv.zip"的压缩文件中。总之,Pandas提供了灵活且方便的方法来写入和处理各种类型的文件,包括Excel、CSV等。\[3\] #### 引用[.reference_title] - *1* [PythonPandas的常用技能【写入数据】](https://blog.csdn.net/weixin_42575020/article/details/128850513)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [在Python使用pandas进行文件读取和写入方法详解](https://blog.csdn.net/m0_59485658/article/details/123818977)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值