I want to mean normalize my data frame, when I implement the first version of code I am getting the normalized values, but when I implement version 2 I am getting an error called stop iteration
. ["1B","2B","3B","HR","BB"]
are columns in my data frame.
Version 1:
def meanNormalizeRates(df):
subRates = df[["1B","2B","3B","HR","BB"]]
df[["1B","2B","3B","HR","BB"]] = subRates - subRates.mean(axis=0)
return df
stats = stats.groupby('yearID').apply(meanNormalizeRates)
stats.head()
Version 2:
def mean(df):
for val in ["1B","2B","3B","HR","BB"]:
stats[val] = stats[val] -stats[val].mean(axis=0)
stats = stats.groupby('yearID').apply(mean)
stats.head()
I couldnt understand the difference between the two versions.
A good example
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9],
'gate' : [9, 7, 4,6, 9]}
frame = pd.DataFrame(data)
frame.head()
Version 1.1
def std(df):
temp = df[['gate', 'pop']]
df[['gate', 'pop']] = temp - temp.mean(axis=0)
return df
frame.groupby('year').apply(std)
gate pop state year
0 9 1.5 Ohio 2000
1 7 1.7 Ohio 2001
2 4 3.6 Ohio 2002
3 6 2.4 Nevada 2001
4 9 2.9 Nevada 2002
Version 1.2
def mean(df):
for val in ['gate', 'pop']:
df[val] = df[val]- df[val].mean(axis=0)
frame.groupby('year').apply(mean)
error: stop iteration