工作中要读取excel中的数据,然后插入Mysql中,但是mysql对于空值的要求,pands里只能是None值才能插入,否则会报这样的错误:
pymysql.err.ProgrammingError: nan can not be used with MySQL
网上搜到的处理方式是:
df_new = df_new.where(pd.notnull(df_new), None)
这个确实可以,但是!!!
对于时间类型的和数值类型的,一直不成功,解决方法是————
**先把这俩类型转换成字符类型,然后再加一行
> df_new = df_new.where(pd.notnull(df_new), None)
就OK了!!!
还有碰到pandas里面是NaT格式的,可以用如下方式处理:
df_new ['col'].replace({pd.NaT: None}, inplace=True)