Javatpoint Logo
Javatpoint Logo

Pandas shift()

If you want to shift your column or subtract the column value with the previous row value from the DataFrame, you can do it by using the shift() function. It consists of a scalar parameter called period, which is responsible for showing the number of shifts to be made over the desired axis. It is also capable of dealing with time-series data.

Syntax:

Parameters:

  • periods: It consists of an integer value that can be positive or negative. It defines the number of periods to move.
  • freq: It can be used with DateOffset, tseries module, str or time rule (e.g., 'EOM').
  • axis: 0 is used for shifting the index, whereas 1 is used for shifting the column.
  • fill_value: Used for filling newly missing values.

Returns

It returns a shifted copy of DataFrame.

Example1: The below example demonstrates the working of the shift().

Output

   a_data    b_data   c_data 
0   NaN       NaN      NaN
1   NaN       NaN      NaN
2   45.0      26.0     22.0
3   28.0      37.0     19.0
4   39.0      41.0     11.0

Example2: The example shows how to fill the missing values in the DataFrame using the fill_value.

Output

   a_data    b_data   c_data 
0    70       70       45
1    70       70       28
2    70       70       39
3    70       70       32
4    70       70       18

Next TopicPandas.replace()




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA