Monica Monica - 3 months ago 9
Python Question

Calculating mean value in DataFrame using a mask

I got the following DataFrame:

DATA Price1 Price2 Price3
sys dis
27 0.8 43.89 83.06 33.75
0.9 2.56 12.19 2.48
1.0 42.28 1.87 1.93
1.2 22.70 1.41 3.64
1.4 20.38 1.36 2.02
28 0.8 22.024 35.47 16.96
0.9 2.69 36.41 19.33
1.0 59.30 8.90 11.41
1.2 25.08 4.55 11.99
1.4 26.85 3.30 7.37
1.6 437.82 3.50 5.65
1.8 55.21 2.91 1.84
2.0 32.54 4.68 5.03
2.5 52.91 5.42 6.58


I need to calculate mean Prices for dis < 1.0 and seperately for dis > 1.0.

I've tried to create a mask function:

def mask(df):
df.loc[df.index.get_level_values('dis').between(0.8,1.0), 'Price1'].mean()
df.loc[df.index.get_level_values('dis').between(1.0,2.6), 'Price1'].mean()
return df

print (df_new.ix[:,'Price1']).apply(mask)


Thought I am getting the following error: AttributeError: ("'Float64Index' object has no attribute 'between'")

Answer

You could use boolean comparators:

mean_low = df.loc[(df.index.get_level_values('dis') < 1.0), 'Price1'].mean()
mean_high = df.loc[(df.index.get_level_values('dis') > 1.0), 'Price1'].mean()
Comments