Reputation: 85
I have time series data, I want to append a column that gives the difference between the value(x_i) and the mean(x_avg).
df
time col1 newcolumn
1 30 0
2 20 -10
3 50 20
4 20 -10
where mean of col1 = (30+20+50+20)/4 = 30. thanks.
Upvotes: 0
Views: 25
Reputation: 1364
You can use apply function. But you need to store the average of that column separately.
def diff(row):
global mean
return row.col1 - mean
dict = {'col1':[30,20,50,20]}
df = pd.DataFrame(dict)
mean = df.col1.mean()
df['diff'] = df.apply(diff, axis=1)
print(df)
col1 diff
0 30 0.0
1 20 -10.0
2 50 20.0
3 20 -10.0
Upvotes: 1