TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas shift()

Pandas shift() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas DataFrame.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:

DataFrame.shift(periods=1, freq=None, axis=0)

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().

import pandas as pd
info= pd.DataFrame({'a_data': [45, 28, 39, 32, 18],
'b_data': [26, 37, 41, 35, 45],
'c_data': [22, 19, 11, 25, 16]})
info.shift(periods=2)

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.

import pandas as pd
info= pd.DataFrame({'a_data': [45, 28, 39, 32, 18],
'b_data': [26, 38, 41, 35, 45],
'c_data': [22, 19, 11, 25, 16]})
info.shift(periods=2)
info.shift(periods=2,axis=1,fill_value= 70)

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 TopicDataFrame.sort()




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf