mercredi 16 juin 2021

Pandas Conditional Statement for using multiple columns

I have been trying to get the following conditional using a pandas dataframe to get a calculation:

The logic is below:

If df[column1] is > 10 and df[column2] >10 and df[column3] >10 then df[new_column] = weighted average of the three

else df[new_column] is minimum of two column values.

My Code:

if (df[df['Count1']] >= 10) and (df['Count2'] >= 10) and (df['Count3'] >= 10):
    
    df['base_mean'] = ( (df['Count1 value'] * df['Count1 weight']) + (df['Count2 value'] * df['Count2 weight']) + (df['Count3 value'] * df['Count3 weight']) / (df['Count1'] + df['Count2'] + df['Count3'])

else:
    df['base_mean'] = np.minimum(df['Count1 value'], df['Count2 value'])

Every time I try to write this out in python I get a Key error.

Thank you,

Aucun commentaire:

Enregistrer un commentaire