samedi 3 juillet 2021

How to use Python Pandas isin() in the context of an IF statement

Having some trouble working out the issue here and could use some advise.

col_list = ['Task1','Task2']
        
if new_temp_df['Task Type'].isin(col_list):    ### THIS IS THE OFFENDING LINE!

     temp_list2.append(new_temp_df['Job Number'])
     results2_df = results2_df.append({
                            'Job Number': new_temp_df['Job'],
                            'Task1': 'Yes',
                            'Task1 Runs': new_temp_df['Runs'],
                            'Task2': 'Yes',
                            'Task2 Runs': new_temp_df['Runs'],
                            'Campaign-level Match': 'Yes',
                            }, ignore_index=True)

else:
    pass ..........

I keep getting the error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I've tried variants to suit (based on abit of googling), for example

if new_temp_df['Task Type'].isin(col_list).all()

and

if np.where(new_temp_df['Task Type'].isin(col_list))

No joy.

Desired End Goal

  1. To read in a dataframe where all the values in field 'Task Type' are checked against the list provided in [col_list].
  2. If both those values in the [col_list] variable are present - TRUE, If not: FALSE
  3. Have that evaluated at the IF statement level - return the bool value (and hopefully all just get on with our lives happily ever after)

Please, any ideas would be great. I see isin() used in all capacities but not in an IF statement - very interested to know the method.

Thanks all in advance

Aucun commentaire:

Enregistrer un commentaire