TypeError: bad operand type for unary ~: 'float'

后端 未结 1 1020
清酒与你
清酒与你 2020-12-21 23:09

I have a weird looking dataframe that I need to wrangle. It looks something like this:

   Unnamed: 0       REFERENCE_CODE  ... Unnamed: 12  Unnamed: 13
0            


        
相关标签:
1条回答
  • 2020-12-21 23:32

    You can fillna first:

    ~df.REFERENCE_CODE.fillna('').str.isnumeric()
    

    Example:

    In [11]: s = pd.Series(['1', np.nan, 'c'])
    
    In [12]: s
    Out[12]:
    0      1
    1    NaN
    2      c
    dtype: object
    
    In [13]: s.str.isnumeric()
    Out[13]:
    0     True
    1      NaN
    2    False
    dtype: object
    
    In [14]: ~s.str.isnumeric()
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-14-2e51f8bd1622> in <module>()
    ----> 1 ~s.str.isnumeric()
    
    ~/.miniconda3/lib/python3.7/site-packages/pandas/core/generic.py in __invert__(self)
       1141     def __invert__(self):
       1142         try:
    -> 1143             arr = operator.inv(com._values_from_object(self))
       1144             return self.__array_wrap__(arr)
       1145         except Exception:
    
    TypeError: bad operand type for unary ~: 'float'
    
    In [15]: ~s.fillna('').str.isnumeric()
    Out[15]:
    0    False
    1     True
    2     True
    dtype: bool
    
    0 讨论(0)
提交回复
热议问题