• pymysql写入时,遇到pandas dataframe中有混合的数据类型以及nan值


    当遇到nan值无法写入mysql数据库,如果加上

    df[pd.isna(df)]=None

    又碰到"Cannot do inplace boolean setting on mixed-types with a non np.nan value"。可以乖乖认怂。认怂方式之一:

    1. def importExcelToMysql(cur, objt):
    2. query = """REPLACE INTO origContactInfo (deviceRecordKey, fax, phone, email, importTime, updateTime) VALUES (%s, %s, %s, %s, %s, %s)"""
    3. for parent, dirnames, filenames in os.walk(objt.path_url):
    4. for filename in filenames:
    5. print(filename)
    6. excel_path = os.path.join(objt.path_url, filename)
    7. # 读取excel文件
    8. workbook = read_excel_with_file_name(excel_path)
    9. worksheet = workbook[objt.contact_sheet_name]
    10. row_count = len(worksheet)
    11. # 将表中每一行数据读到sqlstr数组中
    12. for i in range(1, row_count):
    13. current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    14. valuestr = worksheet.loc[i].values.tolist()
    15. #########################认怂大法在此###################################
    16. for find_nan in range(0,len(valuestr)):
    17. if pd.isna(valuestr[find_nan]):
    18. valuestr[find_nan]=""
    19. ######################################################################
    20. valuestr.append(current_time)
    21. valuestr.append(current_time)
    22. valuestr = tuple(valuestr)
    23. # 将每行数据存到数据库中
    24. cur.execute(query, valuestr)
    25. print(filename + " finished writing to DB!")

  • 相关阅读:
    Neo4j _sparql语言
    Django与Ajax
    行业案例|指标中台如何助力银行业普惠金融可持续发展
    linux shell脚本读取并解析yaml文件
    趣学算法 —— 兔子数列
    echart柱状图y坐标轴反转问题
    MySQl表的增删查改(CRUD)
    【大数据】【Spark】Spark入门
    12_清醒思考艺术书摘
    java线程池介绍
  • 原文地址:https://blog.csdn.net/u011410413/article/details/126451882