Skip to content
Advertisement

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

enter image description here>

df = pd.DataFrame(pd.read_csv("SPOILER_Monster_Hunt.csv"))
df.columns = ["S/No", "Member_Name", "L1", "L2", "L3", "L4", "L5"]
#some modifications below
df = df.drop(index=0)
df = df.drop(columns='S/No')
df = df.fillna(0)

 #Adjusting points with monster levels

df['L1'] = df['L1'] * 50
df['L2'] = df['L2'] * 150
df['L3'] = df['L3'] * 300
df['L4'] = df['L4'] * 1000
df['L5'] = df['L5'] * 2000

df.head()
# show the dataframe
print(df)

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:

#Read the dataframe from csv file.
df = pd.read_csv('location')
#Perform all the necessary edits and modifications.
df.columns = ["S/No", "Member_Name", "L1", "L2", "L3", "L4", "L5"]
df = df.drop(index=0)
df = df.drop(columns='S/No')
df = df.fillna(0)

print(df.dypes) # This will display the datatype of each column in the dataframe.

#Editing the values based on monster level.
df['L1'] = df['L1'].astype(int)
df['L1'] = df['L1'] * 50
df['L2'] = df['L2'].astype(int)
df['L2'] = df['L2'] * 150
df['L3'] = df['L3'].astype(int)
df['L3'] = df['L3'] * 300
df['L4'] = df['L4'].astype(int)
df['L4'] = df['L4'] * 1000
df['L5'] = df['L5'].astype(int)
df['L5'] = df['L5'] * 2000

#display the top 5 rows of your dataframe
df.head()

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().

columns_to_int = ['L1','L2','L3','L4','L5']
for col in columns_to_int:
    df[col] = df[col].astype(int)

df['L1'] = df['L1'] * 50
df['L2'] = df['L2'] * 150
df['L3'] = df['L3'] * 300
df['L4'] = df['L4'] * 1000
df['L5'] = df['L5'] * 2000

df.head()

Hope this helped.

Advertisement