“字段列表”python pandas 中的未知列“nan”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45395729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unknown column 'nan' in 'field list' python pandas
提问by whitebear
I am using pandas (0.20.3)
and python 3.5.3
我正在使用pandas (0.20.3)
和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.
我认为这是因为 mysql 不理解 'nan' 作为 mull。
According to this article
根据这篇文章
The problem was fixed in pandas 0.15.0
该问题已在 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()
回答by J_Scholz
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 NaN
s in your DataFrame:
问题中链接的文章指的是您未在代码中使用的 DataFrame.to_sql()。如果要保持这种写入数据库的方式,则需要更改NaN
DataFrame中的s:
As explained in this question, the solution is, to change all NaN
-values to None
:
如本问题所述,解决方案是将所有值更改NaN
为None
:
stocksDf = stocksDf.where((pd.notnull(stocksDf)), None)
Further important annotation from the original answer:
来自原始答案的进一步重要注释:
This changes the dtype of all columns to object
.
这会将所有列的 dtype 更改为object
.
回答by Trinity
Your data might have newline characters or escape sequencesanywhere. This is considered as 'nan' by MySQL. So manually adjust such a situation or use another pieces of code from pandas to remove escaped characters
您的数据可能在任何地方都有换行符或转义序列。这被 MySQL 视为“nan”。所以手动调整这种情况或者使用pandas中的另一段代码去除转义字符
回答by verisimilidude
Pandas read_csv function can put (at least) two types of nulls into your data, numpy.float64('nan')
and numpy.nan
. There are cases where one is acceptable and the other isn't. This sounds like one of those cases. Use the debugger to determine what the type()
of the data is. You can then 'fix' the data with (DataFrame.replace
)[http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html#pandas-dataframe-replace]
Pandas read_csv 函数可以将(至少)两种类型的空值放入您的数据中,numpy.float64('nan')
并且numpy.nan
. 在某些情况下,一种是可以接受的,另一种是不能接受的。这听起来像是其中一种情况。使用调试器来确定type()
数据是什么。然后,您可以使用 ( DataFrame.replace
)[ http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html#pandas-dataframe-replace]“修复”数据