Reputation: 85
I have dataframe columns with floats and NaN's and need to format some of them with 0 decimal places and some of them two decimal places. Currently to get the 0 decimal places I am using the code below:
df['myCol'] = df['myCol'].fillna(-1)
df['myCol'] = df['myCol'].astype(int)
df['myCol'] = df['myCol'].astype(str)
df['myCol'] = df['myCol'].replace('-1', '')
Is there a faster way to do this and what is the best way for formatting the columns where two decimal places are necessary?
Upvotes: 2
Views: 465
Reputation: 2222
You could use df.round()
df['myCol'] = df['myCol'].round(decimals=2)
Upvotes: 1