-2

Given a dataframe:

DF_NBA.head(4)

enter image description here

I want to calculate the average like above for MeanGScore. and its applicable for n number of rows.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Edg
  • 27
  • 4
  • Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael May 15 '18 at 12:52
  • Could you expand you example data? With a input and expected output? – ak_slick May 15 '18 at 12:53
  • sorry for the confusion. the Input is HScore and I want to add new column name MeanGScore that has the value as above. Its like getting the mean value incremental – Edg May 15 '18 at 12:59

1 Answers1

1

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;
Matthew
  • 369
  • 3
  • 17