我試圖將回歸的結果寫回MySQL,但是在遍歷擬合值並獲取NaNs寫入爲空值時出現問題。本來,我做了反覆這樣說:迭代並將熊貓Dataframe NaNs寫回到MySQL
for i in dataframe:
cur = cnx.cursor()
query = ("UPDATE Regression_Data.Input SET FITTEDVALUES="+(dataframe['yhat'].__str__())+" where timecount="+(datafrane['timecount'].__str__())+";")
cur.execute(query)
cnx.commit()
cur.close()
.....這說SQL thew回我:
"mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NaN'
所以,我一直在試圖通過篩選出的NaN只要求Python的時候yhat不等於NaN的承諾:
for i in dataframe:
if cleandf['yhat']>(-1000):
cur = cnx.cursor()
query = ("UPDATE Regression_Data.Input SET FITTEDVALUES="+(dataframe['yhat'].__str__())+" where timecount="+(datafrane['timecount'].__str__())+";")
cur.execute(query)
cnx.commit()
cur.close()
但後來我得到這個:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
所以,我儘量避開它與這在我上面的語法:
if cleandf['yhat'][i]>(-1000):
但當時得到這樣的:
ValueError: Can only tuple-index with a MultiIndex
然後嘗試添加itterows()同時爲:
for i in dataframe.iterrows():
if cleandf['yhat'][i]>(-1000):
但是得到與上面相同的問題。
我不知道我在做什麼錯在這裏,但假設它是在Pandas DataFrames中迭代的東西。但是,即使我獲得了正確的迭代,我也希望將Null寫入NaN出現的SQL中。
那麼,你覺得我應該怎麼做?
您是否嘗試過使用'write_frame'和'read_frame',就像在[這個答案](http://stackoverflow.com/questions/14431646/how-to-write-pandas-dataframe-to-sqlite-with-指數/ 14432914#14432914)? –