Maximiliano Vazquez
Maximiliano Vazquez

Reputation: 334

if value in pandas column is not in

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!

Upvotes: 0

Views: 198

Answers (1)

Akanksha Atrey
Akanksha Atrey

Reputation: 880

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.

Edit after comments to iterate over subset of the dataframe:

df_subset = Total_unificado[~Total_unificado['Casa'].isin(['StockCenter', 'Dexter', 'Moov'])]

for index, row in df_subset.iterrows():
   apply conditions...

Upvotes: 1

Related Questions