Home > other >  if value in panndas column is not in
if value in panndas column is not in

Time:05-11

I have a df named DF. I need to apply some conditions if values of "Casa" column are not in ('StockCenter', 'Dexter', 'Moov') . I'm trying this:

if Total_unificado[~Total_unificado['Casa'].isin(['StockCenter', 'Dexter', 'Moov'])]:

but I´m getting this error:

 File "c:\Users\User\Desktop\Personal\DABRA\Unificador_Salida_Final.py", line 81, in <module>
    if Total_unificado[~Total_unificado['Casa'].isin(['StockCenter', 'Dexter', 'Moov'])]:      
  File "C:\Users\User\Desktop\Personal\DABRA\Scraper_jfs\venv\lib\site-packages\pandas\core\generic.py", line 1527, in __nonzero__
    raise ValueError(
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What is wrong in the script??

thanks in advance!

CodePudding user response:

The output of Total_unificado[~Total_unificado['Casa'].isin(['StockCenter', 'Dexter', 'Moov'])] is the subset of the dataframe where values of "Casa" are not in the specified list. You cannot apply if on a dataframe, hence the error.

CodePudding user response:

Try something like this:

import pandas as pd

print DF[DF['columnname'].astype(str).str.contains('Casa')]
  • Related