Python Pandas Dataframe, remove all rows where 'None' is the value in any column

前端 未结 3 1761
我在风中等你
我在风中等你 2021-02-01 17:02

I have a large dataframe. When it was created \'None\' was used as the value where a number could not be calculated (instead of \'nan\')

How can I delete all rows that h

相关标签:
3条回答
  • 2021-02-01 17:36

    UPDATE:

    In [70]: temp[temp.astype(str).ne('None').all(1)]
    Out[70]:
          0     1  2  3  4  5   6  7
    0  str1  str2  2  3  5  6  76  8
    

    Old answer:

    In [35]: x
    Out[35]:
          a     b   c
    0     1     2   3
    1     4  None   6
    2  None     7   8
    3     9    10  11
    
    In [36]: x = x[~x.astype(str).eq('None').any(1)]
    
    In [37]: x
    Out[37]:
       a   b   c
    0  1   2   3
    3  9  10  11
    

    or bit nicer variant from @roganjosh:

    In [47]: x = x[x.astype(str).ne('None').all(1)]
    
    In [48]: x
    Out[48]:
       a   b   c
    0  1   2   3
    3  9  10  11
    
    0 讨论(0)
  • 2021-02-01 17:43

    Setup
    Borrowed @MaxU's df

    df = pd.DataFrame([
        [1, 2, 3],
        [4, None, 6],
        [None, 7, 8],
        [9, 10, 11]
    ], dtype=object)
    

    Solution
    You can just use pd.DataFrame.dropna as is

    df.dropna()
    
       0   1   2
    0  1   2   3
    3  9  10  11
    

    Supposing you have None strings like in this df

    df = pd.DataFrame([
        [1, 2, 3],
        [4, 'None', 6],
        ['None', 7, 8],
        [9, 10, 11]
    ], dtype=object)
    

    Then combine dropna with mask

    df.mask(df.eq('None')).dropna()
    
       0   1   2
    0  1   2   3
    3  9  10  11
    

    You can ensure that the entire dataframe is object when you compare with.

    df.mask(df.astype(object).eq('None')).dropna()
    
       0   1   2
    0  1   2   3
    3  9  10  11
    
    0 讨论(0)
  • 2021-02-01 17:56

    Thanks for all your help. In the end I was able to get

    df = df.replace(to_replace='None', value=np.nan).dropna()

    to work. I'm not sure why your suggestions didn't work for me.

    0 讨论(0)
提交回复
热议问题