Cactus Philosopher
Cactus Philosopher

Reputation: 864

Iterate through rows in DataFrame and perform diff method only if values in a column are identical

I want to iterate through the rows in a DataFrame and perform the df.columnB.diff() method only if the current row and the next row have the same string in df['columnA'].

How can I do this?

Upvotes: 0

Views: 39

Answers (1)

BENY
BENY

Reputation: 323366

You are looking for groupby

df['New']=df.groupby('columnA')['columnB'].diff()

Upvotes: 1

Related Questions