pymysql insert dataframe 遇到 nan 值

py 复制代码
def get_db_conn():
    """MYSQL连接"""
    host = Config.MYSQL["host"]
    pwd = Config.MYSQL["pwd"]
    user = Config.MYSQL["user"]
    port = Config.MYSQL["port"]
    database = Config.MYSQL["database"]
    conn = pymysql.Connect(host=host, port=port, user=user, passwd=pwd, db=database, charset='utf8')
    return conn

def insert_data(df):
    df = df.where(df.notnull(), None)
    conn = get_db_conn()
    cur = conn.cursor()
    sql = f"""INSERT INTO alg_cluster_info({','.join(df.columns)})
    VALUES({','.join(['%s']*len(df.columns))})"""
    print(sql)
    try:
        cur.executemany(sql, [tuple(x) for x in df.values])
        conn.commit()
    except Exception as err:
        conn.rollback()
        log.error("{}".format(err))
    finally:
        cur.close()
        conn.close()
相关推荐
Codefengfeng1 小时前
Python Base环境中加包的方法
开发语言·python
清水白石0081 小时前
《Python 编程全景解析:从核心精要到测试替身(Test Doubles)五大武器的实战淬炼》
开发语言·python
如若1232 小时前
AutoDL云服务器 NVIDIA 570驱动 EGL渲染修复全记录
运维·服务器·python
甲枫叶2 小时前
【claude】Claude Code正式引入Git Worktree原生支持:Agent全面实现并行独立工作
java·人工智能·git·python·ai编程
清水白石0083 小时前
《Python 编程全景解析:从核心精要到 Hypothesis 属性基测试的边界探索》
开发语言·python
勇往直前plus4 小时前
深入理解 Python 内存模型:模块、类、对象的存储与运行机制
开发语言·python
yunhuibin4 小时前
NIN网络学习
人工智能·python·深度学习·神经网络·学习
派大星-?4 小时前
自动化测试五模块一框架(下)
开发语言·python
两万五千个小时4 小时前
构建mini Claude Code:02 - 把 Bash 拆成专用工具(read_file, write_file 等)
人工智能·python
henry1010105 小时前
Ansible自动化运维全攻略(AI生成)
linux·运维·python·ansible·devops