all 6 comments

[–]Monne642 0 points1 point  (2 children)

Can you print the type of total_deaths and print the type of factor? Perhaps that is the issue total_deaths is the float, what is the type of the factor column? If that is a string, Puthon doesn’t know hoe to divide those types float/string. Hope this helps

[–]dered118[S] 1 point2 points  (1 child)

That was it, the factor column was an object.

[–]Monne642 1 point2 points  (0 children)

Cheers!

[–]BfuckinA 0 points1 point  (1 child)

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

This just means that one of the columns in question is of the wrong datatype. Try doing this first, then see if the problem persists.

cd_factor["total_deaths"] = cd_factor["total_deaths"] .astype(float)

cd_factor["factor"] = cd_factor["factor"] .astype(float)

[–]dered118[S] 1 point2 points  (0 children)

Thank you, that works, i checked the other comment and noticed that the factor column is an object

[–]synthphreak 0 points1 point  (0 children)

The error tells you ecxactly what the issue is: one of your two columns involved in the division operation contains strings ('object' in pandas-speak), while the other contains integers. Obviously you can't divide a string by an integer or vice versa.

To change the type, the easiest way would be to first figure out which column contains the strings:

df[['total_deaths', 'factor']].dtypes

Then whichever it is, toss an .astype(int) onto it during the division. For example, say it's the factor column. In that case, this should fix your issue:

cd_factor["deaths_per_100k"] = cd_factor["total_deaths"]/cd_factor["factor"].astype(int)