0

How do I get the average from multiple rows where column stage = 2.

At the moment I am using

average = df.loc[df.Stage == 2,'Vout'].mean()

However, this returns an average based off the entire column.

I want to have multiple average values based off certain rows, as there is multiple blocks of data.

Sample Data Data Set

Any help would be great! Thanks.

5
  • How looks expected output from sample data in question? Commented Dec 6, 2021 at 13:18
  • I will insert the average values into a new excel sheet. Commented Dec 6, 2021 at 13:31
  • You want the mean of rows 5, 7 & 8 (1054, 1031, 1031) separate from the mean of rows 12, 14 & 15 (2, 1046, 1040)? Commented Dec 6, 2021 at 13:31
  • Yes @Paul, as there will be multiple blocks of data just like these. Commented Dec 6, 2021 at 13:41
  • You will need to assign a specific value to these blocks. so you can group by them, I believe @jezrael did it in his answer. Commented Dec 6, 2021 at 13:47

1 Answer 1

1

If possible distinguish group by missing values use:

df['g'] = df['Stage'].isna().cumsum()

average = df.loc[df.Stage == 2].groupby('g')['Vout'].mean()
Sign up to request clarification or add additional context in comments.

1 Comment

Using this I get ``` cannot convert g ```

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.