Skip to content
Advertisement

Python mutiply pandas dataframe containing scientific and float values with a float

import pandas as pd 

df=pd.DataFrame({‘years’:[‘Y0’,‘Y1’,’Y2’,’Y3’],‘price1’:[0.01, 0.0029,0.00368,0.00439536],
‘price2’:[0.02, 0.001, 9.161781253394068e-04,0.001718]})

df.iloc[1:,1:] = df.iloc[1:,1:].mul(0.75)

It’s getting error due to scientific values in df, please let me know if any one has any suggestions or help to fix this?

Error:

TypeError: Could not operate 0.75 with block values can’t multiply sequence by non-int of type ‘float’

Advertisement

Answer

I guess you’re getting this issue because of type. you can try to convert the column to float type –

import pandas as pd

df=pd.DataFrame({'years':['Y0','Y1','Y2','Y3'],'price1':[0.01, 0.0029,0.00368,0.00439536], 'price2':[0.02, 0.001, 9.161781253394068e-04,0.001718]})

# converting dtype
df['price1'] = pd.to_numeric(df['price1'], errors='coerce')
df['price2'] = pd.to_numeric(df['price2'], errors='coerce')
df.iloc[1:,1:] = df.iloc[1:,1:].mul(0.75)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement