zoukankan      html  css  js  c++  java
  • pandas里面过滤列出现ValueError: cannot index with vector containing NA / NaN values错误的解决方法(转)

    ###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')

     

  • 相关阅读:
    Pycharm2019.1.2安装详解
    下载及安装Python详细步骤
    ClientDataSet中撤消与恢复操作及执行细节
    Delphi KeyPress KeyDown时Key的值
    fastReport动态传参【含图片】
    Delphi静态和动态调用dll的实例
    关于SPC系统
    SQL Server 占用内存太高,查找占用内存高以及影响其性能的sql语句
    SqlServer 资源占用情况[简易]
    Sqlserver DMV诊断和调优DB性能
  • 原文地址:https://www.cnblogs.com/Lee-yl/p/11419395.html
Copyright © 2011-2022 走看看