[英]Updating part of column in Pandas data frame with a Numpy array
I have data in a format close to that of df (shown below). 我的数据格式接近df(如下所示)。 My problem now is that I want to populate the data in avg_value with the average value for the past "days_back" days.
我现在的问题是我想用过去“ days_back”天的平均值填充avg_value中的数据。
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 来自R,这将是一件容易的事。但是,当我尝试做
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!
我敢打赌,有一个真正简单的解决方案可以解决这个问题,但是我找不到它:/任何帮助将不胜感激!
You are really close, need specify column in loc
: 您真的很接近,需要在
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 以及为什么需要它更好地解释返回视图还是副本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.