Getting average of selected rows for each column pandas
NickName:fudgey Ask DateTime:2021-12-13T23:41:56

Getting average of selected rows for each column pandas

I want to be able to get the average of the last 4 rows that are in 'Stage' 2, for each column.

Sample Data

Currently I am using av = df.loc[df['Stage'] == 2, 'Vout'].mean() but this gives me the average for the entire column. So for the column vout I am getting the entire columns average value, when I just want the columns average value to be the average of the last 4 rows that are in stage 2.

Copyright Notice:Content Author:「fudgey」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70337244/getting-average-of-selected-rows-for-each-column-pandas

More about “Getting average of selected rows for each column pandas” related questions

Getting average of selected rows for each column pandas

I want to be able to get the average of the last 4 rows that are in 'Stage' 2, for each column. Sample Data Currently I am using av = df.loc[df['Stage'] == 2, 'Vout'].mean() but this gives me the

Show Detail

Average of selected rows in csv file

In a csv file, how can i calculate the average of selected rows in a column: Columns I did this: import numpy as np import matplotlib.pyplot as plt import pandas as pd #Read the csv file: df = pd.

Show Detail

Pandas: Calculate the average every 2 rows of a column and put it into the a new column

I want to make an average of a column, but I want the averages to be put into a new column with pandas. I want to go from this format: values 10 5 8 7 2 5 6 7 To this format: values av

Show Detail

Pandas Rows MODE, AVERAGE Python

I have a pandas dataframe with a list of products in rows, and the columns are the sales of current month, current month - 1, current month -2 and current month - 3 for all rows I want to add a new

Show Detail

Calculating the average value for every 10 cells in each column by pandas

In my excel csv files, there are around 1500 rows and 30 columns. I believe I can use python to complete it. so here is my target: How to let python read my excel file correctly. I want to reduce ...

Show Detail

How to calculate the average of specific values in a column in a pandas data frame?

My pandas data frame has 11 columns and 453 rows. I would like to calculate the average of the values in rows 450 to 453 in column 11. I would then like to add this 'average value' as a new column ...

Show Detail

pandas apply with different arg for each column/row

Assume I have a M (rows) by N (columns) dataFrame df = pandas.DataFrame([...]) and a vector of length N windows = [1,2,..., N] I would like to apply a moving average function to each column in ...

Show Detail

Getting average of column in pandas

I'm trying to be able to read a file where I will pull what the name of the location is and then calculate the average amount of snow they get. This is what I have so far. import pandas data = pan...

Show Detail

pandas get column average for rows with a certain value?

I can't get the average of values of a column in pandas based on the values of a different column. For example >>> df ID city timeDiff 0 1 A 2 1 ...

Show Detail

Dropping rows and finding the average of a speific column

I am trying to remove specific rows from the dataset and find the average of a specific column after the rows are removed without changing the original dataset import pandas as PD import NumPy as n...

Show Detail