I am trying to change a column (price) which is an object data type to use in a groupby. Changing this column to float sometimes keeps the same number of decimal places as the original, sometimes rounds to one decimal place, and sometimes gets rid of all decimal places altogether. I would like to keep the float values the same as the original due to eventual reconciliation that needs to be accurate to the decimal place. I have tried changing the column type using astye, and also pd.to_numeric. Ideally, price_3 and price_4 should apples should be 93927.82. Any help would be greatly appreciated.
import pandas as pd
d = {'product': ['apples', 'pears', 'grapes', 'oranges'],
'price': ['$93,927.83' , '$9,868.23', '$110,838.10', '$10,093.88']}
df = pd.DataFrame(data=d)
df['price_2'] = df['price'].str.replace('$', '').str.replace(',', '').str.replace('(', '').str.replace(')', '')
df['price_3'] = df['price_2'].astype(float)
df['price_4'] = pd.to_numeric(df['price_2'])