Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown column 'nan' in 'field list' python pandas

I am using pandas (0.20.3) and python 3.5.3

I have error like this

mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'nan' in 'field list'

I thought it is because of mysql doesn't understand 'nan' as mull.

According to this article

The problem was fixed in pandas 0.15.0

However I still have this error. is there something wrong with my cord??

Or where should I fix??

stocksDf = pd.read_csv('companys.csv', names=['name','place'])

for i,row in stocksDf.iterrows():
    sql = "insert into CompanyUs(name,place) VALUES(%s,%s)" 
    data = (row['name'],row['place'])
    cur.execute(sql,data)
    pprint("Company Write : %s" % row['name'])
    conn.commit()
like image 892
whitebear Avatar asked Jul 30 '17 02:07

whitebear


1 Answers

The article linked in the question is referring to DataFrame.to_sql() which you are not using in your code. If you want to maintain this way of writing to the database, you need to change the NaNs in your DataFrame:

As explained in this question, the solution is, to change all NaN-values to None:

stocksDf = stocksDf.where((pd.notnull(stocksDf)), None)

Further important annotation from the original answer:

This changes the dtype of all columns to object.

like image 72
J_Scholz Avatar answered Oct 07 '22 22:10

J_Scholz