In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how to use the shift() method of python DataFrame. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take a small series to show you how to use the shift() method of python DataFrame.
In python data analysis, you can use the shift() method to move the position of the DataFrame object's data forward and backward.
syntax
DataFrame.shift(periods=1, freq=None, axis=0)
Periods can be understood as the number of times of movement amplitude, shift defaults to 1 unit at a time, and also defaults to 1 time (periods defaults to 1), then the length of movement is 1 * periods.
Periods can be positive or negative. Negative numbers indicate lag, positive numbers indicate lag.
freq is an optional parameter, defaults to None, and can be set to a timedelta object. This applies when the index is time series data.
When freq is None, the values of other data are shifted, i.e., periods*1 unit length is shifted.
When freq position None, the value of the time series index is shifted, and the length of the shift is periods * freq unit length.
axis defaults to 0, indicating alignment. If it is a row, it means to operate on the row.
The default value for movement hysteresis without a corresponding value is NaN.
example
period is positive, no freq
import pandas as pdpd.set_option ('display.unicode.east_asian_width', True)data = [51.0, 52.33, 51.21, 54.23, 56.78]index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']df = pd.DataFrame (data=data, index=index, columns=='close '])df.index.name ='date'print(df)print("==============================")df ='close '].shift()df ='change'] = df ='close '].shift()print(df)
period is negative, no freq
import pandas as pdpd.set_option('display.unicode.east_asian_width', True)data = [51.0, 52.33, 51.21, 54.23, 56.78]index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']index = pd.to_datetime(index)index.name = 'date'df = pd.DataFrame(data=data, index=index, columns=='yesterday closed'])print(df)print("============================")df ='close '] = df ='yesterday closed'].shift(-1)df ='change'] = df ='yesterday closed']. shift (-1) - df ='close ']print(df)
period is positive, freq is positive
import pandas as pdimport datetimepd.set_option('display.unicode.east_asian_width', True)data = [51.0, 52.33, 51.21, 54.23, 56.78]index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']index = pd.to_datetime(index)index.name = 'date'df = pd.DataFrame(data=data, index=index, columns=['close'])print(df)print("=========================================")print(df.shift(periods=2, freq=datetime.timedelta(3)))
As shown in the figure, the time series data of the index column lags by 6 days. (2 times 3)
period is positive, freq is negative
import pandas as pdimport datetimepd.set_option('display.unicode.east_asian_width', True)data = [51.0, 52.33, 51.21, 54.23, 56.78]index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']index = pd.to_datetime(index)index.name = 'date'df = pd.DataFrame(data=data, index=index, columns=['close'])print(df)print("=========================================")print(df.shift(periods=3, freq=datetime.timedelta(-3)))
As shown in the figure, the time series data of the index column is delayed by 9 days (three times negative three)
period is negative, freq is negative
import pandas as pdimport datetimepd.set_option('display.unicode.east_asian_width', True)data = [51.0, 52.33, 51.21, 54.23, 56.78]index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']index = pd.to_datetime(index)index.name = 'date'df = pd.DataFrame(data=data, index=index, columns=['close'])print(df)print("=========================================")print(df.shift(periods=-3, freq=datetime.timedelta(-3)))
As shown in the figure, the time series data of the index column lags by 9 days (negative three times negative three)
At this point, I believe that everyone has a deeper understanding of "how to use the shift() method of python DataFrame", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.