How to use apply function for multiplying 2 in a column using pandas

Viewed 27

The below table having 1000 rows but here let's consider 3 rows:

Date B C
2022-07-24 12 1234
2021-02-01 34 6789
2020-04-30 23 4324

I want to multiply all values of B column with 2 and all values of C columns with 3 using apply function.

1 Answers

Try using df.mul:

df.loc[:, ['B','C']] = df[['B','C']].mul([2,3])

         Date   B      C
0  2022-07-24  24   3702
1  2021-02-01  68  20367
2  2020-04-30  46  12972

If set on using apply, you could do:

df.loc[:, ['B','C']] = df[['B','C']].apply(lambda x: x.mul([2,3]), axis=1)

Evidently, this is unnecessary.

Related