Skip to content
Advertisement

Can I multiply an entire column with a scalar in a dataframe in python?

enter image description here>

JavaScript

Now the problem here is that multiplication of a column with a scaler just multiplies the value in that column to appear multiple times for instance df[L1] = df[L1]*50 will make values of L1 repeat 50 times in the entire column but what I want is to multiply each value in that column to be multiplied by 50.

here is the link to dataset I am using

Advertisement

Answer

The reason for this could be that your column dtype is a string object instead of an numerical one. In python, multiplying a string with a scalar ‘n’ just repeats the string ‘n’ times.

You can check the datatype of each column by calling the df.dtypes attribute.

You can convert your column datatype into the required type using:

JavaScript

Alternate solution for overall cleaner code

If there are multiple rows that you need to change the type of, you can create a function to do so and call it on the required rows using df.apply().

JavaScript

Hope this helped.

Advertisement