3

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!

1 Answer 1

4

You are really close, need specify column in loc:

df.loc[df["DAY"] == the_day_I_want].avg_value = my_numpy_array

what is same as:

df.loc[df["DAY"] == the_day_I_want]['avg_value'] = my_numpy_array

change to:

df.loc[df["DAY"] == the_day_I_want, 'avg_value'] = my_numpy_array

And why need it better explain returning a view versus a copy

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, man! Works like a charm :) Can't believe I was that close all this time -.-
I think it is super you was close, not necessary change a lot of code ;) Nice day and glad can help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.