0

I have a dataframe with 2 columns, 'A' and 'B', both consisting of values between 0 and 1.

How do I, for example, calculate the mean of the values in column 'B' when the value in column 'A' is between 0 and 0.1, ignoring the other values?

1
  • df.loc[df.B.between(0,0.1),'A'].mean() Commented Aug 4, 2020 at 13:50

1 Answer 1

3

You can use boolean indexing:

df.loc[df['A'].between(0,0.1), 'B'].mean()

Or in this case, since the values are already >=0, you can just do:

df.loc[df['A'].le(0.1), 'B'].mean()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.