I have a dataframe with 2 columns. Speed(A) and elevation (B). I want to get the average of column (A) based on a range in column (B)
for example: i want to select all rows in (B) that have a value of between -0.5 and 0.8 and get the average of the speed in (A)
Can anyone help?
CodePudding user response:
If you only need a scalar for the mean of column 'A'
where certain rows based on column 'B'
are matched, you can use .loc
which you can read more about here:
For example:
df = pd.DataFrame({'A': [1,2,3,4,5], 'B' : [-1,0,0.1,0.2,1]})
>>> df
A B
0 1 -1.0
1 2 0.0
2 3 0.1
3 4 0.2
4 5 1.0
Then the following line evaluates the average of column A subset by a some condition for B:
>>> df.loc[df['B'].between(-0.5,0.8) , 'A'].mean()
3.0