I have a pandas dataframe, in which some columns have numeric values while others don't, as shown below:
City a b c
Detroit 129 0.54 2,118.00
East 188 0.79 4,624.4712
Houston 154 0.65 3,492.1422
Los Angeles 266 1.00 7,426.00
Miami 26 0.11 792.18
MidWest 56 0.24 772.7813
I want to round off these numeric values to 2 decimal places, for which I am using:
df = df.replace(np.nan, '', regex=True)
After which df becomes:
City a b c
Detroit 129.0 0.54 2,118.0
East 188.0 0.79 4,624.47
Houston 154.0 0.65 3,492.14
Los Angeles 266.0 1.0 7,426.0
Miami 26.0 0.11 792.18
MidWest 56.0 0.24 772.78
It works mostly fine, but it also converts proper integers to decimals, i.e., values like 100 are rounded off to 100.0. I want the dataframe like this:
City a b c
Detroit 129 0.54 2,118
East 188 0.79 4,624.47
Houston 154 0.65 3,492.14
Los Angeles 266 1 7,426
Miami 26 0.11 792.18
MidWest 56 0.24 772.28
I want to keep such values as proper integers itself, while rounding off others to 2 decimal places in all the numeric columns. How can I do that?