How to shift a column in a Pandas DataFrame?



We can use the shift() method in Pandas to shift the columns of a DataFrame without having to rewrite the whole DataFrame. shift() takes the following parameters

shift(self, periods=1, freq=None, axis=0, fill_value=None)
  • periods  Number of periods to shift. It can take a negative number too.
  • axis  It takes a Boolean value; 0 if you want to shift index and 1 if you want to shift column
  • fill_value  It will replace the missing value.

Let's take an example and see how to use this shift() method.

Steps

  • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
  • Print the input DataFrame, df.
  • Select a column and shift it by using df["column_name]=df.column_name.shift()
  • Print the updated DataFrame.

Example

 import pandas as pd df = pd.DataFrame( dict( name=['John', 'Jacob', 'Tom', 'Tim', 'Ally'], marks=[89, 23, 100, 56, 90], subjects=["Math", "Physics", "Chemistry", "Biology", "English"] ) ) print "Input DataFrame is:\n", df df["name"] = df.name.shift(1) print "After shifting column name by 1:\n", df df["marks"] = df.marks.shift(2) print "After shifting column marks by 2:\n", df df["subjects"] = df.subjects.shift(-1) print "After shifting column subjects by -1:\n", df

Output

 Input DataFrame is:      name   marks   subjects 0    John    89         Math 1   Jacob    23      Physics 2     Tom   100    Chemistry 3     Tim    56      Biology 4    Ally    90      English After shifting column name by 1:      name   marks   subjects 0     NaN    89         Math 1    John    23      Physics 2   Jacob   100    Chemistry 3     Tom    56      Biology 4     Tim    90      English After shifting column marks by 2:      name   marks   subjects 0     NaN    100       Math 1    John    100    Physics 2   Jacob     89  Chemistry 3     Tom     23    Biology 4     Tim    100    English After shifting column subjects by -1:      name   marks   subjects 0     NaN   100      Physics 1    John   100    Chemistry 2   Jacob   89      Biology 3 Tom 23 English 4 Tim 100 NaN
Updated on: 2021-09-15T07:23:13+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements