# Python | Pandas dataframe.shift()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas` dataframe.shift()` function Shift index by desired number of periods with an optional time freq. This function takes a scalar parameter called period, which represents the number of shifts to be made over the desired axis. This function is very helpful when dealing with time-series data.

Syntax:DataFrame.shift(periods=1, freq=None, axis=0)
Parameters :
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’}

Return : shifted : DataFrame

Example #1: Use `shift()` function to shift the index axis by 2 periods in a time-series data

 `# 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 ``=` `pd.date_range(``'01 / 01 / 2000'``, periods ``=` `5``, freq ``=``'12H'``) ` `  `  `# Creating a dataframe with 4 columns ` `# using "ind" as the index for our dataframe ` `df ``=` `pd.DataFrame({``"A"``:[``1``, ``2``, ``3``, ``4``, ``5``],  ` `                   ``"B"``:[``10``, ``20``, ``30``, ``40``, ``50``], ` `                   ``"C"``:[``11``, ``22``, ``33``, ``44``, ``55``], ` `                   ``"D"``:[``12``, ``24``, ``51``, ``36``, ``2``]},  ` `                    ``index ``=` `ind) ` ` `  `# Print the dataframe ` `df ` Lets use the `dataframe.shift()` function to shift the index axis by 2 periods in positive direction

 `# shift index axis by two periods in positive direction ` `# axis = 0 is set by default ` `df.shift(``2``, axis ``=` `0``) ` Lets shift the index axis in negative direction by some periods

 `# shift index axis by two periods in negative direction ` `# axis = 0 is set by default ` `df.shift(``-``2``, axis ``=` `0``) `

Output : Example #2: Use `shift()` function to shift the column axis by 2 periods in a time-series data

 `# 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 ``=` `pd.date_range(``'01 / 01 / 2000'``, periods ``=` `5``, freq ``=``'12H'``) ` `  `  `# Creating a dataframe with 4 columns ` `# using "ind" as the index for our dataframe ` `df ``=` `pd.DataFrame({``"A"``:[``1``, ``2``, ``3``, ``4``, ``5``],  ` `                   ``"B"``:[``10``, ``20``, ``30``, ``40``, ``50``],  ` `                   ``"C"``:[``11``, ``22``, ``33``, ``44``, ``55``],  ` `                   ``"D"``:[``12``, ``24``, ``51``, ``36``, ``2``]},  ` `                    ``index ``=` `ind) ` ` `  `# Print the dataframe ` `df ` Lets use the `dataframe.shift()` function to shift the column axis by 2 periods in positive direction

 `# shift column axis by two periods in positive direction ` `df.shift(``2``, axis ``=` `1``) ` Lets shift the column axis in negative direction by some periods

 `# shift column axis by two periods in negative direction ` `df.shift(``-``2``, axis ``=` `0``) `

Output : My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6385243,"math_prob":0.89790386,"size":3933,"snap":"2020-24-2020-29","text_gpt3_token_len":1064,"char_repetition_ratio":0.18503436,"word_repetition_ratio":0.53731346,"special_character_ratio":0.30841598,"punctuation_ratio":0.16776751,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99505305,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],"im_url_duplicate_count":[null,null,null,7,null,7,null,null,null,7,null,7,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-09T15:53:10Z\",\"WARC-Record-ID\":\"<urn:uuid:6cc35fa1-d098-4c15-a1fa-6bb5a7459a56>\",\"Content-Length\":\"136605\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1d3520d8-0ff1-4c31-b55f-684fc578d3dd>\",\"WARC-Concurrent-To\":\"<urn:uuid:92eedc23-87b2-4111-b574-c949c833a6ec>\",\"WARC-IP-Address\":\"168.143.241.155\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/python-pandas-dataframe-shift/\",\"WARC-Payload-Digest\":\"sha1:SJ3MSX4V4UUC5XLRAPAPUT2SMNH2VYWG\",\"WARC-Block-Digest\":\"sha1:QRAUYLBPZRUCYDRAMGUPLOLLTERPS7MW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593655900335.76_warc_CC-MAIN-20200709131554-20200709161554-00573.warc.gz\"}"}