在 pandas 数据框中添加行移位

编程入门 行业动态 更新时间:2024-10-26 11:19:08
本文介绍了在 pandas 数据框中添加行移位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个熊猫 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 10:52:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框中   数据   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!