×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Determining when a column value changes in pandas dataframe

Learn, how to determine when a column value changes in pandas dataframe?
Submitted by Pranit Sharma, on December 09, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Suppose we are given a data frame with two columns and we need to find a way so we can determine that the change happened between the two rows.

Determining when a column value changes

For this purpose, we will first create a data frame with 2 columns A and B, E and then we will create a new column C. We will use the diff() method with column B and store the result in column C. This will assign 1 and 0 values in column C which represents where the values are changed and where not.

We will then filter the data according to a specific condition.

Let us understand with the help of an example,

Python program to determine when a column value changes in pandas dataframe

# Importing pandas
import pandas as pd

# Creating a dictionary
d = {
    'A':[1,2,3,4,5],
    'B':[1,2,3,4,5]
}

# Creating a dataframe
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Creating a new column
df['C'] = df['B'].diff()

# Filter result
res = df[df['C'] != 0]

# Display result
print("Result:\n",res,"\n")

Output

The output of the above program is:

Example: Determining when a column value changes in pandas dataframe

Python Pandas Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.