1

How to exclude rows if any of the columns in a dataframe contains non-binary (0 or 1) numbers. Not to exclude based on a specific column, but for all of the columns.

For example, we have a dataframe like this, and we want to delete row number 0, 2 and 4.

array = [[1, 0, 0, 0, 0, 0, 2],
         [1, 0, 0, 1, 0, 0, 0],
         [2, 0, 0, 1, 0, 0, 0],
         [0, 0, 0, 0, 0, 1, 0],
         [1, 0, 0, 3, 0, 0, 0]]
df = pd.DataFrame(array, columns = ['AB','AC','CB','DE','FA','CD','AF'] )

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Jiamei
  • 405
  • 3
  • 14

2 Answers2

2
df = df[df.isin([1, 0])].dropna()

# display(df)
    AB  AC  CB   DE  FA  CD   AF
1  1.0   0   0  1.0   0   0  0.0
3  0.0   0   0  0.0   0   1  0.0
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0
  • Another Method is
df = df[~df.isin([0, 1])]

  • where ~ means not
  • Also we can use .dropna() at the end of the code, Instead of ~

Other Useful Resources

Vikas Ukani
  • 105
  • 4
  • 16