Home > Mobile >  Divide multiple columns by a fix number in pandas
Divide multiple columns by a fix number in pandas

Time:02-18

How can I divide multiple columns by a fixed number?

   A    B    C       D
0  1  100     2000  10
1  2  200     3000  00
2  3  300     4000  20
3  4  400     5000  40
4  5  500     4000  24
5  6  600     2000  23

I would like to dived each number in column "B" and "C" by 1000 and get new DataFrame with having other columns unchanged.

CodePudding user response:

You can use broadcasting:

df[['B','C']] /= 1000

Output:

   A    B    C   D
0  1  0.1  2.0  10
1  2  0.2  3.0   0
2  3  0.3  4.0  20
3  4  0.4  5.0  40
4  5  0.5  4.0  24
5  6  0.6  2.0  23

CodePudding user response:

You can do it this way:

df.B=df.B/1000
df.C=df.C/1000
  • Related