1

I have 2 columns in excel. I get an error from simply dividing them with pandas. What am I doing wrong?

import pandas as pd
import numpy as np

df = pd.read_excel('day.xlsx', sheet_name = 'day1')
df=pd.DataFrame(df)
df=df.rename(columns={0: 'Time', 'null': 'Temp', 'null.1':'Pressure'})
df['ratio']=df['Temp']/df['Pressure']

I am trying to create a third column 'ratio' from dividing 'Temp' with 'Pressure'.

Error message:

TypeError: unsupported operand type(s) for /: 'float' and 'str'

petezurich
  • 9,280
  • 9
  • 43
  • 57
PandasKoala
  • 75
  • 1
  • 10

1 Answers1

0

The error message is explicit:

TypeError: unsupported operand type(s) for /: 'float' and 'str'

It means that in df['Temp']/df['Pressure'] some (or all) of the values of df['Pressure'] are strings. You must first convert them to float, normally with astype(float) if all values represent acceptable float values or with to_numeric(coerce=True) if you want to be super safe: any non convertible string will be changed to NaN:

df['ratio']=df['Temp']/pd.to_numeric(df['Pressure'], coerce=True)

If you still get errors unsupported operand type(s) for /: 'str' and 'float', you will have to do the same conversion on df['Temp'].

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252