pandas dataframe dataframe.shift() 函数-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > python >

pandas dataframe dataframe.shift() 函数

作者:迹忆客 最近更新:2024/04/24 浏览次数:

pandas dataframe.shift 方法用于将 dataframe 的索引按指定的周期数移位,时间频率可选。


pandas.dataframe.shift() 语法

dataframe.shift(periods=1, freq=none, axis=0, fill_value=none)

参数

periods 整数。决定移动索引的周期数,可以是负数,也可以是正数
freq dateoffsettseries.offsetstimedeltastr。可选参数,用于在不调整数据的情况下移动索引值
axis 沿着行(axis=0)或列(axis=1)移动
fill_value 用于新引入的缺失值的标量值

返回值

它返回一个带有移位索引值的 dataframe 对象。


示例代码:dataframe.shift() 函数沿行移动

import pandas as pd
df = pd.dataframe({'x': [1, 2, 3,],
                   'y': [4, 1, 8]})
print("original dataframe:")
print(df)
shifted_df=df.shift(periods=1)
print("shifted dataframe")
print(shifted_df)

输出:

original dataframe:
   x  y
0  1  4
1  2  1
2  3  8
shifted dataframe
     x    y
0  nan  nan
1  1.0  4.0
2  2.0  1.0

这里,我们将 periods 的值设置为 1,这将使 dataframe 的行从顶部向底部移动 1 个单位。

在向底部移动的同时,最上面的行成为空缺,默认由 nan 值填充。

如果我们想将行从底部向顶部移动,我们可以将 periods 参数设置为负值。

import pandas as pd
df = pd.dataframe({'x': [1, 2, 3,],
                   'y': [4, 1, 8]})
print("original dataframe:")
print(df)
shifted_df=df.shift(periods=-2)
print("shifted dataframe")
print(shifted_df)

输出:

original dataframe:
   x  y
0  1  4
1  2  1
2  3  8
shifted dataframe
     x    y
0  3.0  8.0
1  nan  nan
2  nan  nan

它将行从底部向顶部移动,周期为 2


示例代码:dataframe.shift() 函数沿列移动

如果我们想移动沿列轴移动,我们在 shift() 方法中设置 axis=1

import pandas as pd
df = pd.dataframe({'x': [1, 2, 3,],
                   'y': [4, 1, 8]})
print("original dataframe:")
print(df)
shifted_df=df.shift(periods=1,axis=1)
print("shifted dataframe")
print(shifted_df)

输出:

original dataframe:
   x  y
0  1  4
1  2  1
2  3  8
shifted dataframe
    x    y
0 nan  1.0
1 nan  2.0
2 nan  3.0

在这里,我们将 periods 的值设置为 1,这将使 dataframe 的列轴从左向右移动 1 个单位。

如果我们想将列轴从右向左移动,我们为 periods 参数设置一个负值。

import pandas as pd
df = pd.dataframe({'x': [1, 2, 3,],
                   'y': [4, 1, 8]})
print("original dataframe:")
print(df)
shifted_df=df.shift(periods=-1,axis=1)
print("shifted dataframe")
print(shifted_df)

输出:

original dataframe:
   x  y
0  1  4
1  2  1
2  3  8
shifted dataframe
     x   y
0  4.0 nan
1  1.0 nan
2  8.0 nan

它将列轴从右向左移动了 1 个周期。


示例代码:dataframe.shift 方法,参数为 fill_value

在前面的例子中,移位后的缺失值默认用 nan 来填充,我们也可以通过使用 fill_value 参数用其他值而不是 nan 来填充。我们也可以通过使用 fill_value 参数用其他值而不是 nan 来填充缺失值。

import pandas as pd
df = pd.dataframe({'x': [1, 2, 3,],
                   'y': [4, 1, 8]})
print("original dataframe:")
print(df)
shifted_df=df.shift(periods=-1,
                    axis=1,
                    fill_value=4)
print("shifted dataframe")
print(shifted_df)

输出:

original dataframe:
   x  y
0  1  4
1  2  1
2  3  8
shifted dataframe
   x  y
0  4  4
1  1  4
2  8  4

它将所有由 shift() 方法创建的缺失值用 4 填充。

上一篇:pandas pandas.melt() 函数

下一篇:没有了

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

发布时间:2024/04/24 浏览次数:894 分类:python

了解如何在 python 中将 pandas 系列日期时间转换为字符串

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图