Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple conditional average python

Tags:

python

pandas

What I'm trying to do is a multiple conditional average. It means that I have a list of x variables (of the same length) and I'd like to compute the average of one variable conditioned to the value/range of the others.

The file is: It is a sample file

The code I tried is:

wb=pd.ExcelFile('file.xlsx')
wb.sheet_names
df=wb.parse('Sheet1')
df[:]
Var1=df['Col1_Name']
Var2=df['Col2_Name']
Var3=df['Col3_Name']
Var4=df['Col4_Name']
Var5=df['Col5_Name']
Var6=df['Col6_Name']

if (Var1 == 0).any() and (Var2 == 0).any() and (Var3 < 0.8).any() and (Var6 == 0).any():
    print sum(Var4)/len(Var4)

It seems to be ok, but, if I change the conditions, the result is always the same. Moreover I tried to calculate the same on Excel as double check and the result is indeed different.. Could you help me? Thank you :)

like image 246
user6608923 Avatar asked Feb 20 '26 23:02

user6608923


1 Answers

What you have written seems all sorts of wrong. I believe what you want is:

(Var1 == 0).all() and (Var2 == 0) and (Var3 >= 0).all() and (Var3 < 1).all() and (Var6 == 0).all()
like image 193
piRSquared Avatar answered Feb 23 '26 13:02

piRSquared