search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | slice_shift method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.slice_shift(~) method shifts the rows of the DataFrame by the specified amount, and empty rows are stripped away.

NOTE

Both methods slice_shift(~) and shift(~) perform shifting, but there are two main differences:

  • slice_shift(~) does not return a new copy of the source DataFrame, that is, modifying the result of slice_shift(~) will end up mutating the source DataFrame. In contrast, shift(~) returns a new copy.

  • slice_shift(~) may modify the shape of the DataFrame, whereas the shift(~) method always returns a DataFrame with the same shape as the original. Check examples below for clarification.

Parameters

1. periodslink | int

The amount to shift by:

  • positive integers: rows are shifted downwards

  • negative integers: rows are shifted upwards.

Return Value

A DataFrame with the rows shifted.

Examples

Consider the following DataFrame:

df = pd.DataFrame({"A":["a","b","c","d"],"B":["e","f","g","h"],"C":["i","j","k","l"]}, index=[6,7,8,9])
df
A B C
6 a e i
7 b f j
8 c g k
9 d h l

Basic usage

To shift down by 2 rows:

df.slice_shift(2)
A B C
8 a e i
9 b f j

Notice how only the values are shifted down, while the index is left as is. Since indexes 6 and 7 no longer have valid values, those rows are stripped away.

To contrast this with the shift(~) method:

df.shift(2)
A B C
6 NaN NaN NaN
7 NaN NaN NaN
8 a e i
9 b f j

The takeaway here is that, instead of throwing away indexes 6 and 7, shift(~) simply fill their values with NaN.

Shifting upwards

To shift up by 2 rows:

df.slice_shift(-2)
A B C
0 c g k
1 d h l

Mutating behaviour

Consider the same DataFrame as above:

df = pd.DataFrame({"A":["a","b","c","d"],"B":["e","f","g","h"],"C":["i","j","k","l"]}, index=[6,7,8,9])
df
A B C
6 a e i
7 b f j
8 c g k
9 d h l

Performing the shift and storing the returned DataFrame in x:

x = df.slice_shift(2)
x
A B C
8 a e i
9 b f j

If we modify x, then the original df is mutated as well:

x.iloc[0,0] = "Z"
df
A B C
6 Z e i
7 b f j
8 c g k
9 d h l
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!