Home > database >  Perform diff() on a subset of dataframe columns
Perform diff() on a subset of dataframe columns

Time:12-11

I have a dataframe with a dozen columns and I'd like to do a diff() on only about half of them, keeping the result unchanged.

Currently I've got:

df = ... # input dataframe

diff_cols = ['fizz', 'buzz', 'foo', 'bar', 'baz']

for dc in diff_cols:
    df[dc] = df[dc].diff()

Is there a canonical approach?

CodePudding user response:

We usually do

diff_cols = ['fizz', 'buzz', 'foo', 'bar', 'baz']
df[diff_cols] = df[diff_cols].diff()
  • Related