Multiplying two pandas fields
dataframe, pandas, python
Solution
It's because it's a `str` change it to float using `astype`:
transactions['exec_price'] = transactions['exec_price'].astype(float)
or safer use `to_numeric` to handle any duff values:
transactions['exec_price'] = pd.to_numeric(transactions['exec_price'], errors='coerce')
When you multiply a str column it just repeats the string contents by that scalar value
Problem
I have a dataframe with a list of transactions and I would like to take the exec_price field and multiply it with size field so that I can get the total cost. So, I would like to add a new field: cost = exec_price * size ``` date security action size ask_price exec_price \ order_id 188 2016-10-24 10:04:44 AAPL buy 5 116.599998 117.44 189 2016-10-24 10:04:44 NFLX buy 5 127.5 128.4 190 2016-10-24 10:04:44 AMD buy 5 6.52 6.65 status order_id 188 filled 189 filled 190 filled ``` When I try to multiple, using: ``` transactions['cost'] = transactions['exec_price'] * transactions['size'] ``` I get this: ``` date security action size ask_price exec_price \ order_id 188 2016-10-24 10:04:44 AAPL buy 5 116.599998 117.44 189 2016-10-24 10:04:44 NFLX buy 5 127.5 128.4 190 2016-10-24 10:04:44 AMD buy 5 6.52 6.65 status cost order_id 188 filled 117.44117.44117.44117.44117.44 189 filled 128.4128.4128.4128.4128.4 190 filled 6.656.656.656.656.65 ``` Anyone knows how I can fix this? Thanks in advance.