Home > Software design >  Remove multiple rows based on condition in multiple columns in pandas
Remove multiple rows based on condition in multiple columns in pandas

Time:07-05

I have dataset like this from csv file.enter image description here

I want to remove all records that has question mark ('?') in any of their column. I tried this code:

for column in df.columns:
    df = df[df[column] != '?']

But it does not work, here is the output.enter image description here My expected output is index 1 and 3 get removed. How can i achieve that?

CodePudding user response:

Filter all rows without ? with space:

out = df[(df != ' ?').all(axis=1)]

Or if possible use read_csv and no missing values, only ? use:

 out = pd.read_csv(file, na_values=' ?').dropna()
  • Related