【问题标题】:Row filtering so that we only keep finite entries行过滤,以便我们只保留有限的条目
【发布时间】:2014-07-18 06:36:54
【问题描述】:

我发现 this recipe 在我的数据框中保留有限的条目。

公式为:

df[df == np.Inf] = np.NaN
df.dropna()

但是,当我尝试时:

In: df[df == np.Inf] = np.NaN

## -- End pasted text --
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-88eed8630e79> in <module>()
----> 1 df[df == np.Inf] = np.NaN

/Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/core/frame.pyc in __setitem__(self, key, value)

/Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/core/frame.pyc in _setitem_frame(self, key, value)

TypeError: Cannot do boolean setting on mixed-type frame

有没有更好的方法来过滤行,以便我们只在特定列中保留有限个条目?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    按照here 的建议,您可以使用mode.use_inf_as_null

    In [14]: df = DataFrame({'a': randint(3,size=10)})
    
    In [15]: df['b'] = tm.choice([2,3,nan,inf,-inf], size=len(df))
    
    In [16]: df
    Out[16]:
       a       b
    0  1     inf
    1  2    -inf
    2  0  3.0000
    3  1    -inf
    4  2     NaN
    5  1  3.0000
    6  1     inf
    7  0  2.0000
    8  2    -inf
    9  2     inf
    
    In [17]: with pd.option_context('mode.use_inf_as_null', True):
       ....:     res = df.dropna()
       ....:
    
    In [18]: res
    Out[18]:
       a  b
    2  0  3
    5  1  3
    7  0  2
    

    【讨论】:

      【解决方案2】:

      使用np.isinf()

      x = pandas.DataFrame([
          [1, 2, np.inf],
          [4, np.inf, 5],
          [6, 7, 8]
      ])
      x[np.isinf(x)] = np.nan
      print(x)
      
         0   1   2
      0  1   2 NaN
      1  4 NaN   5
      2  6   7   8
      

      那么x.dropna() 给了我:

         0  1  2
      2  6  7  8
      

      要仅查看列的子集,请使用 subset kwarg(始终获取列表):

      x.dropna(subset=[1])
         0   1   2
      0  1   2 NaN
      2  6   7   8
      

      您也可以听取 DSM 的建议,直接索引数据框: x[~np.isinf(x).any(axis=1)]

      【讨论】:

      • 您也可以使用isinf 而不使用它来替换值,例如x[~np.isinf(x).any(axis=1)].
      • 嗯 @DSM 我明白了:TypeError: ufunc 'isinf' not supported for the input types
      • 向我展示一个示例数据框和代码来重现它,我们应该能够使用平方。 (@user815423426)
      猜你喜欢
      • 1970-01-01
      • 2017-10-21
      • 2014-05-13
      • 1970-01-01
      • 2021-08-04
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多