大熊猫中的回归列

时间:2015-06-22 14:18:14

标签: python pandas linear-regression

假设我有一个pandas数据帧df,其中包含一些简单的索引,例如: 0,1,2,...和只有一列'Values',其中包含数值数据。我想添加一个新列'Trend',以便df['Trend'][n]是基于原始值列中的5个连续值的线性回归系数:'df['Values'][n-5] ... 'df['Values'][n-1] 。前5个值当然是未定义的,所以我们假设它们是NaN。有没有一种简单的方法可以做到这一点?

1 个答案:

答案 0 :(得分:2)

您是否尝试过检查index并执行不同的操作,具体取决于您在DataFrame中的位置?

for index, row in df.iterrows():
    if index<5:
        df['Trend'][index] = 'NaN'
    else:
        df['Trend'][index] = df['Values'][index-5]...