简体   繁体   中英

Multiple conditional average python

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: 这是一个示例文件

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 :)

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM