
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- How to rename column names in a Pandas DataFrame?
- How to delete a column from Pandas DataFrame
- How to delete a column from a Pandas DataFrame?
- How to sort a column of a Pandas DataFrame?
- Python - How to select a column from a Pandas DataFrame
- Apply uppercase to a column in Pandas dataframe
- Python - Add a zero column to Pandas DataFrame
- Apply uppercase to a column in Pandas dataframe in Python
- Python – Create a new column in a Pandas dataframe
- Python - Add a prefix to column names in a Pandas DataFrame
- How to count the NaN values in a column in a Python Pandas DataFrame?
- Merge Pandas DataFrame with a common column
- Python - How to Count the NaN Occurrences in a Column in Pandas Dataframe?
- How to add column from another DataFrame in Pandas?
- How to replace NaN values by Zeroes in a column of a Pandas DataFrame?

Advertisements