I am trying to convert a string to a float in multiple columns. The string is enclosed in parentheses but I need to convert to a negative float in order to do some operations.
Fund Open Activity Gain (Loss) Close
Manager 1 10049217.19 -784717.34 237452.94 9501952.79
Manager 2 11035433.41 0 171126.7 11206560.11
Manager 3 26117736 0 326914.02 26444650.02
Manager 4 3682923.8 -260472.43 33999.86 3456451.23
Manager 5 0 17696136.53 143115.18 17839251.71
Manager 6 20036136.53 -20036136.53 0 0
'Open', 'Activity', and 'Close' are all objects
I have tried stack overflow and reg expressions but I still only get NaNs at the string I am trying to convert.
Here is the code I tried
cols = df1[['Open', 'Activity', 'Gain (Loss)', 'Close']]
for col in cols:
df1[col].replace( '[)]','', regex=True ).replace( '[(]','-',
regex=True ).astype(float)
df1[col] = pd.to_numeric(df1[col], errors='coerce')
print(df1)
When i run this code I still get NaN and it messes up my operations.