I am trying to change the output format of columns in a dataframe without changing the data types.
import pandas as pd
df = pd.DataFrame({'name': ['Johnny', 'Brad'],
'rate': [0.02, 0.035],
'wage': [50000.0, 35000.0]})
print(df.dtypes)
df
which results in the following output
name object
rate float64
wage float64
dtype: object
name rate wage
0 Johnny 0.020 50000.0
1 Brad 0.035 35000.0
I am trying to change the formats without changing the datatypes
df['rate'] = df['rate'].astype(float).map("{:.2%}".format)
df['wage'] = df.apply(lambda x: "{:,}".format(x['wage']), axis=1)
print(df.dtypes)
df
Unfortunately the result is this
name object
rate object
wage object
dtype: object
name rate wage
0 Johnny 2.00% 50,000.0
1 Brad 3.50% 35,000.0
So now if I try to do calculations, they fail. For Example:
df['wage'].sum()
Produces this result
'50,000.035,000.0'
How do I change the print formats, but keep the dtypes as floats?