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

Viewed 4784

I have a function

def cruise_fun(speed, accl, acmx, dcmx):
    count = 0
    index = []
    for i in range(len(speed.dropna())):
        if ((speed[i]>40) & (accl[i]<acmx*0.2) & (accl[i]>dcmx*0.2)):
            count +=1
            index.append(i)
                    
    return count, index

This function is being called in the following statement

cruise_t_all, index_all =cruise_fun(all_data_speed[0], acc_val_all[0], acc_max_all, decc_max_all)

all_data_speed and acc_val_all are two dataframes of 1 column and 38287 rows. acc_max_all and decc_max_all are two float64 values. I have tried to implement solutions provided in stackoverflow as much as I could. I have used both and and &. I can not get around the problem.

1 Answers

You are using pandas in the wrong way. You should not loop over all the rows like you do. You can concatenate the columns provided and then check the conditions:

def cruise_fun(speed, accl, acmx, dcmx):
    df = pd.concat([speed.dropna(), accl], axis=1)
    df.columns = ["speed", "accl"]
    mask = (df["speed"] > 40) & df["accl"].between(dcmx * .2, acmx * .2, inclusive=False)
    return mask.sum(), df[mask].index

NB: A few assumptions that I make:

  • I assume that you do not have conflicts for your column names, otherwise the concat will not work and you will need to rename your columns first
  • I assume that the index from speed.dropna() and accl match but I would not be surprised if it is not the case. You should make sure that this is fine, or better: store everything in the same dataframe
Related