Search code examples
pythonpandasfilterpandas-locisin

Pandas filter with Loc but exclude values in rows


1vat.loc[(vat['Sum of VAT'].isin([np.nan, 0])) &
2        (vat['Comment'] == 'Transactions 0DKK') &
3        (vat['Memo (Main)'] != '- None -'), 'Comment'] = 'Travel bill'
4vat[vat["Comment"] == "Travel bill"]

I have a problem with line 3. It seems not to be working properly when I'm trying to exclude a value in a certain column. The output from the data frame is too large and it's incorrect. are there any other alternatives than using !=, I don't understand why it doesn't work? If I were to use == then it works correctly but not when using !=, please help.


Solution

  • After many tests, I think that line 3 works after testing different codes like:

    (vat['Memo (Main)'].isin(['- None -'] ) == False), 'Comment'] = 'Travel bill'
    

    OR:

    (~vat['Memo (Main)'].isin(['- None -'])), 'Comment'] = 'Travel bill'
    

    I have to backtrack and see why the data frame is not giving me the expected output.