【发布时间】:2020-10-14 20:12:16
【问题描述】:
我有一个具有以下值的数据框 (df)。注意下面的项目代码是字符串“None”
YEAR QUARTER ID AMOUNT DATE COMMENTS PROJECT_CODE
0 2020 3 ABC 300.0 2020-10-07 N/A ABC_0001
0 2020 3 ABC 500.0 2020-10-07 N/A None
我想在使用以下查询插入 oracle 表之前将“无”转换为无:
con3 = cx_Oracle.connect(connstr)
cur3 = con3.cursor()
rows3 = [tuple(x) for x in df.values]
cur3.executemany('''merge into O_TABLE
using dual
on (YEAR = :1 and QUARTER = :2 and ID = :3 and AMOUNT = :4 and DATE = :5 and COMMENTS = :6 and PROJECT_CODE = :7)
when not matched then insert values (:1, :2, :3, :4, :5, :6, :7)
where :3 IS NOT NULL''',rows)
con3.commit()
cur3.close()
con3.close()
我想在 Oracle 表上插入一个空值来代替“无”。我尝试在插入之前将“无”转换为Nan,但这似乎会导致错误:
df['PROJECT_CODE'] = df['PROJECT_CODE'].replace('None', np.nan)
Error:
Traceback (most recent call last):
File "./test_program.py", line 266, in <module>
where :3 IS NOT NULL''',rows3)
TypeError: expecting string or bytes object
如何为我的 df 中的所有“无”值在 oracle 表中插入一个真正的空值?我不想使用 to_sql,因为这限制了我编写 sql 查询的方式。谢谢。
【问题讨论】:
-
或者你可以使用
DataFrame.to_sql()将 nan 转换为 null