Search code examples
pandasnumpydataframemask

Why does't my pandas indexer work when I tried to filter just two values?


I tried to use a indexer to filter just two values (1 and 2) from a DataFrame, but if I check the .csv file I found some 77 values.

#My len is 15333, this is because of "77" values, the correct it will be 15286, i.e taking account just 1 and 2 values. PD:All in 'HAD_CPOX' are int64 and I tried to use != 77.


Solution

  • If I look at your code and you only want to take values HAD_CPOX==1 or HAD_CPOX==2, then your still have to assign your filtered df back to variable df again like this:

    df = df[(df['HAD_CPOX'] == 1) & (df['HAD_CPOX'] == 2)]
    

    You could also write:

    df = df[df['HAD_CPOX'].isin([1, 2])
    

    In your code you only have:

    df[(df['HAD_CPOX'] == 1) & (df['HAD_CPOX'] == 2)]
    

    But then your df is not assigned, so nothing is changed or filtered then from your dataframe.