Drop observations from the data frame in python
Question: How to delete observation from data frame in python. For example, I have data
frame with variables a, b, c in it, and I vat to delete observation if
variable a is missing, or variable c is equal to zero.


Answer: You could build a boolean mask using `isnull`:

    
    
    mask = (df['a'].isnull()) | (df['c'] == 0)
    

and then select the desired rows with:

    
    
    df = df.loc[~mask]
    

`~mask` is the boolean inverse of `mask`, so `df.loc[~mask]` selects rows
where `a` is not null _and_ `c` is not 0.

* * *

For example,

    
    
    import numpy as np
    import pandas as pd
    
    arr = np.arange(15, dtype='float').reshape(5,3) % 4
    arr[arr > 2] = np.nan
    
    df = pd.DataFrame(arr, columns=list('abc'))
    #     a   b   c
    # 0   0   1   2
    # 1 NaN   0   1
    # 2   2 NaN   0
    # 3   1   2 NaN
    # 4   0   1   2
    
    mask = (df['a'].isnull()) | (df['c'] == 0)
    df = df.loc[~mask]
    

yields

    
    
       a  b   c
    0  0  1   2
    3  1  2 NaN
    4  0  1   2
    

