###df_18的字段fuek是否包含 / df_18[df_18['fuel'].str.contains('/')]
报错:
ValueError Traceback (most recent call last) in () 1 # 检查是否已删除 ----> 2 df_18[df_18['fuel'].str.contains('/')]
/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in getitem(self, key) 1956 if isinstance(key, (Series, np.ndarray, Index, list)): 1957 # either boolean or fancy integer index -> 1958 return self.getitemarray(key) 1959 elif isinstance(key, DataFrame): 1960 return self.getitemframe(key)
/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in getitemarray(self, key) 1983 def getitemarray(self, key): 1984 # also raises Exception if object array with NA values -> 1985 if com.is_bool_indexer(key): 1986 # warning here just in case -- previously setitem was 1987 # reindexing but getitem was not; it seems more reasonable to
/opt/conda/lib/python3.6/site-packages/pandas/core/common.py in is_bool_indexer(key) 187 if not lib.is_bool_array(key): 188 if isnull(key).any(): --> 189 raise ValueError('cannot index with vector containing ' 190 'NA / NaN values') 191 return False
ValueError: cannot index with vector containing NA / NaN values
原因:
fuel 中存在 NaN 值,所以得到的结果也是缺失值,而缺失值是不能直接作为索引的。你可以考虑在 contains 中更改 na 参数值,让其变为 False ——Series.str.contains(pat, case=True, flags=0, na=nan, regex=True)
解决:
你可以考虑在 contains 中更改 na 参数值,让其变为 False ——Series.str.contains(pat, case=True, flags=0, na=nan, regex=True) case是False是不区分大小写,而na=False 或 nan是不是有NaN来填充缺失值。 如果这种方法不能解决:先将df_18的空值删除再进行正则化匹配 df_18 = df_18.dropna(how='any')
|