How to find "?" values in a pandas dataframe

I have been trying to read a CSV file in a dataframe which has "?" values in some of the rows.

I want to find the rows which contain these values (?) over all the columns

I tried using loc but it returns an Empty Dataframe

test_df.loc(test_df['rbc'] == "?"]
test_df.loc(test_df['rbc'] == None]

This returns an Empty DataFrame

I want to iterate the dataframe over all the columns

Can someone suggest a way to do this

2 answers

  • answered 2018-01-17 06:19 jezrael

    If want check ? values only in all columns:

    df1 = df.loc[:, (df.astype(str) == '?').any()]
    

    More general if want check all possible substrings ? in all columns:

    df2 = df.loc[:, df.apply(lambda x: x.astype(str).str.contains('\?')).any()]
    

  • answered 2018-01-17 06:19 Nayana Madhu

    This will work.

    result = test_df[test_df['rbc'].str.contains("?")]