python中shift_Python Pandas dataframe.shift()用法及代码示例

本文介绍了Pandas DataFrame的shift()函数,用于在时间序列数据中移动索引和列。shift()接受periods参数指定移动周期,可正可负,并可选择时间频率。通过示例展示了如何在索引轴和列轴上正向和负向移动数据。
摘要由CSDN通过智能技术生成

Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。

Pandas dataframe.shift()函数根据需要的周期数移动索引,并带有可选的时间频率。该函数采用称为周期的标量参数,该参数表示要在所需轴上进行的平移次数。处理时间序列数据时,此功能非常有用。

用法:DataFrame.shift(periods=1, freq=None, axis=0)

参数:

periods: Number of periods to move, can be positive or negative

freq: DateOffset, timedelta, or time rule string, optional Increment to use from the tseries module or time rule (e.g. ‘EOM’). See Notes

axis: {0 or ‘index’, 1 or ‘columns’}

返回:shifted:DataFrame

范例1:采用shift()在时间序列数据中将索引轴移动2个周期的功能

# importing pandas as pd

import pandas as pd

# Creating row index values for our data frame

# We have taken time frequency to be of 12 hours interval

# We are generating five index value using "period = 5" parameter

ind &#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值