在 pandas 数据框中添加行移位 [英] Adding row shifting in pandas dataframe

查看:20
本文介绍了在 pandas 数据框中添加行移位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫 df,它是我通过使用 shift() 函数迭代原始 df 创建的:

I have a pandas df, which I created by using shift() function iterating through the original df:

for i in range(2, 4):
    df["lag_{}".format(i)] = df.x.shift(i)

所以会有实际的 x 列和 lag2-lag10 列,其中 x 值发生了变化.我已经为回归模型训练了这个数据集,以进行一步向前预测.想在数据帧的末尾添加新行,其中 x 的值为 nan 并从最后一个位置移动值,以便能够使用这些新的滞后来拟合模型来预测这个新的 nan 值.如何在熊猫中做到这一点?谢谢!

So there will be actual x column and lag2-lag10 columns with shifted x values. I have trained this dataset for the regression model to make one-step forward prediction. Would like to add the new row in the end of the dataframe with nan value for x and shifted values from the last position to be able to use these new lags for fitting the model to predict this new nan value. How this can be done in pandas? Thanks!

更新:有 df 的图片,未加粗的 df,加粗的要获取的行:

Upd: There is the pic for the df, unbolded-the df, bold-the desired row to get:

推荐答案

使用 DataFrame.append 带有键为 x 的字典:

df = pd.DataFrame({'x':range(10)})

df1 = df.append({'x':np.nan}, ignore_index=True)
#alternative
#df1 = df.append(pd.Series([np.nan], index=['x']), ignore_index=True)

for i in range(2, 10):
    df1["lag_{}".format(i)] = df1.x.shift(i)
print (df1)
      x  lag_2  lag_3  lag_4  lag_5  lag_6  lag_7  lag_8  lag_9
0   0.0    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN
1   1.0    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN
2   2.0    0.0    NaN    NaN    NaN    NaN    NaN    NaN    NaN
3   3.0    1.0    0.0    NaN    NaN    NaN    NaN    NaN    NaN
4   4.0    2.0    1.0    0.0    NaN    NaN    NaN    NaN    NaN
5   5.0    3.0    2.0    1.0    0.0    NaN    NaN    NaN    NaN
6   6.0    4.0    3.0    2.0    1.0    0.0    NaN    NaN    NaN
7   7.0    5.0    4.0    3.0    2.0    1.0    0.0    NaN    NaN
8   8.0    6.0    5.0    4.0    3.0    2.0    1.0    0.0    NaN
9   9.0    7.0    6.0    5.0    4.0    3.0    2.0    1.0    0.0
10  NaN    8.0    7.0    6.0    5.0    4.0    3.0    2.0    1.0

这篇关于在 pandas 数据框中添加行移位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆