jeudi 15 août 2019

If statement nested in For loop - Getting error = The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

I am trying to write a if statement nested in a for loop for my data frame, which looks like the below:

Dataframe screenshot

I want the code to iterate through each row of the dataframe and if it detects "CV22" in the column Detection_Location, it should import one file as dataframe and if it detects "CV23" in column Detection_location, it should import another file as the same dataframe as earlier.

I have tried writing the below code for doing this:

def Get_PHD(df2): if (df2['Detection_Location'] == 'CV22'): PHD_df = pd.read_excel(r'C:\Users\s.gaur\Desktop\LS1 - Edited file.xlsx', sheet_by_name = "Sheet1") return (PHD_df) elif (df2['Detection_Location'] == 'CV23'): PHD_df = pd.read_excel(r'C:\Users\s.gaur\Desktop\LS2 - Edited File.xlsx', sheet_by_name = "Sheet1") return (PHD_df)

for index, row in df2.iterrows(): Get_PHD(df2)

But getting the following error:

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

Can anyone please help as in what i am doing wrong.

Thanks in Advance.

Aucun commentaire:

Enregistrer un commentaire