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,315
  • 2
  • 18
  • 25

2 Answers2

2

You can easily do it like this

df[['A','B','C']] = df[['A','B','C']]/2
Kartikeya Sharma
  • 1,174
  • 1
  • 9
  • 19
1

Try something like this to take advantage of vectorization.

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

ChanMan99
  • 11
  • 1