1

Is there a good way to shorten this:

df.a = df.a / 2
df.b = df.b / 2
df.c = df.c / 2

According to this, something like

df[['A','B','C']] = df[['A', 'B','C']].apply(lambda a: a / 2)

should be avoided.

Zubo
  • 1,543
  • 2
  • 20
  • 26

2 Answers2

2

You can easily do it like this

df[['A','B','C']] = df[['A','B','C']]/2
Kartikeya Sharma
  • 1,335
  • 1
  • 10
  • 22
1

Try something like this to take advantage of vectorization.

df[['A','B','C']]/2

ChanMan99
  • 11
  • 1