Given a dataframe:
DF_NBA.head(4)
I want to calculate the average like above for MeanGScore
.
and its applicable for n
number of rows.
Given a dataframe:
DF_NBA.head(4)
I want to calculate the average like above for MeanGScore
.
and its applicable for n
number of rows.
Quick answer:
import pandas as pd
HScore=[110, 120, 100, 120]
df=pd.DataFrame(data=HScore, columns=['HScore'])
df['MeanGScore']=df['HScore'].expanding().mean()
Or, the "traditional" and slow way:
tempSum=0;
count=0;
for index, row in df.iterrows():
tempSum+=row['HScore'];
count+=1;
df.loc[index,'MeanGScore']=tempSum/count;