I have a dataframe (df) with the below values. Note that the project code below is the String "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
I would like to convert "None" to None before inserting into an oracle table using the below query:
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()
I would like to insert a null value in place of "None" on the Oracle table. I tried converting "None" to Nan before the insert, but this seemed to result in an error:
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
How do i insert a true null into the oracle table for all "None" values in my df? I wouldn't want to use to_sql as that limits the ways I can write my sql query. Thanks.
DataFrame.to_sql()which will convert nan to null