I have data in a format close to that of df (shown below). My problem now is that I want to populate the data in avg_value with the average value for the past "days_back" days.
import numpy as np
import pandas as pd
df = pd.DataFrame({ 'DAY': np.append(np.ones(24),
[np.multiply(np.ones(24), 2),
np.multiply(np.ones(24), 3),
np.multiply(np.ones(24), 4)]),
'value': np.random.randn(1, 24*4)[0],
'avg_value': 0.},
index=pd.date_range('20150101', periods=24*4, freq="H"))
print(df.tail())
DAY avg_value value
2015-01-04 19:00:00 4.0 0.0 0.685153
2015-01-04 20:00:00 4.0 0.0 0.670713
2015-01-04 21:00:00 4.0 0.0 -0.519541
2015-01-04 22:00:00 4.0 0.0 0.795619
2015-01-04 23:00:00 4.0 0.0 -0.150966
Coming from R, this would be an easy thing to do.. But when I try to do
df.loc[df["DAY"] == the_day_I_want].avg_value = my_numpy_array
I get
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
So, as the good boy I am, I proceed with the following
index_row = df.loc[df["DAY"] == the_day_I_want].index
index_col = df.columns.get_loc("avg_value")
df.loc[index_row, index_col] = my_numpy_array
But I still end up with the same error! I bet there is a real easy solution to this problem but I just can't find it :/ Any help would be much appreciated!