Python | Pandas dataframe.slice_shift ()

Python Methods and Functions

The Pandas dataframe.slice_shift() function is equivalent to a shift without copying the data. The displaced data will not include missing periods, and the displaced axis will be smaller than the original. This function simply resets the specified number of periods along the specified axis in the specified direction.

Syntax: DataFrame.slice_shift (periods = 1, axis = 0)

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

Return: shifted: same type as caller

Example # 1: Use slice_shift () to shift the index axis by 2 periods in the temporal data row

# import pandas as pd

import pandas as pd

 
# Create row index values for the data frame
# We took the frequency of the 12-hour interval
# Generate five index values ​​using the period = 5 parameter

ind = pd.date_range ( '01/01 / 2000' , periods = 5 , freq = '12H' )

  
# Create a data frame with 4 columns
# using & quot; ind & quot; as an index to 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 data frame
df

Let's use the dataframe.slice_shift () function to shift the index axis by 2 periods in the positive direction

# shift the index axis by two
# periods in the positive direction
# axis = 0 is set by default

df.slice_shift ( 2 , axis = 0 )

Output:

Notice the index marks, the first two marks are discarded, but the data is offset by two periods in the positive direction.

We can also displace about placing the index in a negative direction for several periods

Output:

Note that in the output, the data points were offset in the negative direction (i.e. up) by 2 periods, and the last two index marks have been removed.

Example # 2: Use slice_shift () to shift the column axis by 2 periods in time series data

# shift the index axis by two
# periods in negative direction
# axis = 0 is set by default

df.slice_shift ( - 2 , axis = 0 )

# import pandas as pd

import pandas as pd

 
# Create line index values ​​for our data frame
# Received frequency at 12 hour intervals
# Generate five values index using period = 5 parameter

 

ind = pd.date_range ( '01/01 / 2000' periods  = 5 , freq = ' 12H' )

  
# Create a data frame with 4 columns
# using & quot; ind & quot; as an index to 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)

 
# shift the column axis by two periods in the positive direction

df.slice_shift ( 2 , axis = 1 )

Output:

As a result, we see that the first two column labels are removed, and the data point along the column axis is offset by 2 periods in the positive direction.

We can also offset the column axis in the negative direction by multiple periods

# shift the column axis by two periods in the negative direction

df.slice_shift ( - 2 , axis = 0 )

Output:

In the output, we can see that the last two column labels have been removed and the data point along the column axis was shifted 2 periods in the negative direction (i.e. left).





Get Solution for free from DataCamp guru