Hani Ihlayyle
Hani Ihlayyle

Reputation: 135

Calculate the average of the rows for each group

I need to calculate the mean of a certain column in DataFrame, so that means for each row is calculated excluding the previous values of the row for which it's calculated in certain group. Lets assume we have this dataframe, this is the expected output

is there any way that like iterate each row by index, adding previous row by index in every iteration, and then calculating mean. I wonder if there's a more efficient way of doing it

unit    A      Expected 
T10     8      8
T10     7      7.5
T10     12     9
T11     10     10
T11     6      8
T12     17     17
T12     7      12
T12     3      9

Upvotes: 1

Views: 264

Answers (3)

folarin
folarin

Reputation: 1

To calculate the mean of a particular column in pandas all you need to do is use the mean method of the pandas library.

mean = df["frequencies"].mean()

where df is the name of the dataframe and frequencies is the column you wish to find the mean of

Upvotes: 0

Scratch'N'Purr
Scratch'N'Purr

Reputation: 10409

You can use expanding:

df2 = df.groupby('unit')['A'].expanding().mean().reset_index()
df['Expected'] = df2['A']

Upvotes: 3

jezrael
jezrael

Reputation: 862741

Divide DataFrameGroupBy.cumsum with counter by GroupBy.cumcount:

g = df.groupby('unit')['A']
df['Expected'] = g.cumsum().div(g.cumcount() + 1)
print (df)
  unit   A  Expected
0  T10   8       8.0
1  T10   7       7.5
2  T10  12       9.0
3  T11  10      10.0
4  T11   6       8.0
5  T12  17      17.0
6  T12   7      12.0
7  T12   3       9.0

Upvotes: 1

Related Questions