Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typecasting before division (or any other mathematical operator) of columns in dataframes

I have a dataframe with columns A, B. I need to add a column C which is basically the division of entries in A by the entries in B.

I tried this:

df['C'] = df['A'] / df['B']

But I need to convert to double or float before I do this. How should I type-cast the dtype of the columns?

Thanks.

like image 496
Navneet Avatar asked Dec 16 '22 20:12

Navneet


1 Answers

How about

df['C'] = df['A'] * 1.0 / df['B']
like image 154
Eero Aaltonen Avatar answered Jan 05 '23 00:01

Eero Aaltonen