Python Pandas How to calculate the average of every other row in a column

SeanK22
import pandas as pd

data = {'Pressure' : [100,112,114,120,123,420,123,1230,132,1,23,13,13,13,123,13,123,3,222,2303,1233,1233,1,1,30,20,40,401,10,40,12,122,1,12,333]}

df = pd.DataFrame(data)

If I had this example DF, is there an easy way to calculate the average of every other row in the column? I would like there to be an average of rows 1,3,5,7... etc and also an average of rows 2,4,6,8,10.... I was not sure what the best way to do this would be.

tlentali

We can get the mean for the even rows this way :

>>> df.iloc[::2].mean() 
Pressure    153.111111
dtype: float64

In the brackets, the syntax is : start(do nothing):stop(do nothing):step_count(2).
So for evens you'd start at 0, go to end, increment by 2.

And we can do the following for the odds rows :

>>> df.iloc[1::2].mean()
Pressure    356.294118
dtype: float64

For odds, we start at 1, go to end, increment by 2.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to calculate the average of a column where the row meets a certain condition in Pandas

Python - Calculate average for every column in a csv file

moving every other row to a new column and group pandas python

Take row means of every other column in pandas (python)

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

Calculate row-wise average pandas python

How to loop through every row, calculate difference and then average everything?

Groupby Year and other column and calculate average based on specific condition pandas

Pandas average every ith row of dataframe subset based on column value

Compute row average by another column in Python/Pandas

Python pandas dataframe, check if column value matches other column value for every row

How to make every other row a column using row below as the value in Pandas?

Calculate mode of a column in Pandas using other column with same row values

pandas:calculate jaccard similarity for every row based on the value in another column

How to take the average of every nth row of Pandas dataframe?

Calculate average of every 7 instances in a dataframe column

Calculate time difference in seconds in pandas if row in other column matches

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

How to group, sum up and calculate a mean of every other element in a column?

Python Pandas: How to take categorical average of a column?

How to calculate row wise average for floats in list in python

Insert every other row to a Column

Python Pandas: Create cumulative average while grouping by other column

Calculate average of month and replace values of other column

Calculate the average of each column in a row at the end

How to calculate the average of a column with DataFrame?

Subsetting every other row in Pandas?

How to average certain values of a column based on other columns condition in pandas

How to compute average every 5 seconds based on time column in python