pandas Python - ValueError:无法索引包含 NA / NaN 值的向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48862235/
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
Python - ValueError: Cannot index with vector containing NA / NaN values
提问by harry04
I'm trying to get the average price of products containing any substrings from a wordlist from a dataframe. I've been able to do so with the following code on multiple spreadsheets -
我试图从数据帧的单词列表中获取包含任何子字符串的产品的平均价格。我已经能够在多个电子表格上使用以下代码来做到这一点 -
dframe['Product'].fillna('', inplace=True)
dframe['Price'].fillna(0, inplace=True)
total_count = 0
total_price = 0
for word in ransomware_wordlist:
mask = dframe.Product.str.contains(word, case=False)
total_count += mask.sum()
total_price += dframe.loc[mask, 'Price'].sum()
average_price = total_price / total_count
print(average_price)
However, one of the spreadsheets throws an error at line -
但是,其中一个电子表格在行处引发错误 -
dframe['Product'].fillna('', inplace=True)
with
和
ValueError: cannot index with vector containing NA / NaN values
I fail to understand why dframe['Product'].fillna('', inplace=True)
isn't handling this problem.
我不明白为什么dframe['Product'].fillna('', inplace=True)
不处理这个问题。
In desperate need of some help! Thanks!
迫切需要一些帮助!谢谢!
回答by jezrael
If first line failed still is possible replace NaN
s in condition in str.contains
by parameter na=False
:
如果第一行仍然失败,则可以通过参数替换NaN
条件中str.contains
的s na=False
:
mask = dframe.Product.str.contains(word, case=False, na=False)
Or try omit inplace=True
and assign back:
或者尝试省略inplace=True
并重新分配:
dframe['Product'] = dframe['Product'].fillna('')