Search code examples
pythonpandasnansklearn-pandas

extract rows which contain only NaN and 0


I have a dataframe which contains NaNs and 0s in some rows for all columns. I am trying to extract such rows, so that I can process them further. Also, some of these columns are object and some float. I am trying the below code to extract such rows, but because of the columns being object, its not giving me the desired result.

Now, I can solve this problem by substituting some arbitrary values to NaN and use it in .isin statement, but then it also changes the datatype of my columns, and I would have to convert them back.

Can somebody please help me with a workaround/solution to this. Thanks.

import pandas as pd
import numpy as np

df = pd.DataFrame({'a':[np.nan,0,np.nan,1,'abc'], 'b':[0,np.nan,np.nan,1,np.nan]})

df

     a   b
0   NaN 0.0
1   0   NaN
2   NaN NaN
3   1   1.0
4   abc NaN
5   NaN 1.0

values = [np.nan,0]
df_all_empty = df[df.isin(values).all(1)]
df_all_empty

Expected Output:

     a   b
0   NaN 0.0
1   0   NaN
2   NaN NaN

Actual Output:

     a  b
0   NaN 0.0

Solution

  • Change

    df_all_empty = df[(df.isnull()|df.isin([0])).all(1)]