问题
Let's say that I have this dataframe and this code to insert my data in the data base :
import pandas as pd
import pyodbc
REFERENCE = ["GZF882348G", "SFGUZBJLNJU", "FTLNGZ242112", "DFBHGVGHG543", "H353464508749","H353464508749","H353464508749","H353464508749", "H353464508749", "H353464508749", "H353464508749"]
IBAN = ["FR57476", "FR57476", "FR57476", "FR57476", "FR57476", "FR57476", " FR57476", "FR57476", "FR57476", "FR57476", "FR57476"]
DATE = ["2020-07-30", "2020-07-30", "2020-07-30", "2020-07-30", "2020-07-30", "2020-07-30", "2020-07-30", "2020-07-30", "2020-07-30", "2020-07-30", "2020-07-30"]
LIB = ["sdf", "dfsf", "dgsg", "dgfsg", "gsdg", "efsg", "efdg", "egsg", "gjtz", "wqeq", "hfgh"]
DEBIT = [289.2, 72.9, 709.23, 0, 97.3, 17.54, 40.32, 6.54, 1.74, '', 12401.04]
CREDIT = ['', '', '', '', '', '', '', '', '', 45, '']
BALANCE = [23.6,23.6,23.6,23.6,56.6,56,56,56,56,87,34]
B = ["CRDT", "CRDT", "CRDT", "CRDT", "DBIT", "DBIT", "DBIT", "DBIT", "DBIT", "CRDT", "DBIT"]
MONTANT = [-2819.2, -782.9, -709.23, 0, -9397.3, -1768.54, -1740.32, -676.54, -81.74, 16250, -12401.04]
df = pd.DataFrame({'Réference' : REFERENCE, 'IBAN' : IBAN, 'Date' : DATE, 'Libelle' : LIB, 'Débit' : DEBIT, 'Crédit' : CREDIT, 'Balance' : BALANCE, 'Balance DrCr':B, 'Montant' : MONTANT})
df[['Débit', 'Crédit', 'Balance', 'Montant']] = df[['Débit', 'Crédit', 'Balance', 'Montant']].apply(pd.to_numeric)
###### -------- Connection -----------------
server = '...'
database = '...'
username = '...'
password = '...'
driver = '...'
connection = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+password)
cursor = connection.cursor()
##### ------- Insert into ----------------
sql_exe = "INSERT INTO dbo.tbl_data_xml (Réference,IBAN,Date,Libelle,Débit,Crédit,Balance,[Balance DrCr],Montant) VALUES (?,?,?,?,?,?,?,?,?)"
# CONVERT DATA TO LIST OF NUMPY ARRAYS
sql_data = df.to_numpy().tolist()
# EXECUTE ACTION QUERY
cursor.executemany(sql_exe, sql_data)
connection.commit()
I have a problem of format to insert this kind of data in my database. The columns "Débit", "Crédit", "Balance" and "Montant" are defined to get floats as data. However the data of these columns are not only integers, I have empty strings too and that is my issue. I know that I have to write a condition that replace a empty string by a "Null" value in the SQL format (the value null in SQL), however I do not know how to do that in python or in SQL. I am discovering/learning the SQL environment.
I do not know if I have to write a code in sql to replace by this value or if I can do it in the python function
Anyone has an idea ?
回答1:
Replace NaN
with None
sql_data = df.replace({np.nan:None}).to_numpy().tolist()
来源:https://stackoverflow.com/questions/65080139/python-and-sql-replacing-the-empty-strings-of-a-dataframe-by-a-null-value-of