TiTo
TiTo

Reputation: 865

Pandas: Setting decimal places for zeros

I have a dataframe showing regression output. I want to style it for publication. I rounded all values (before putting them between parenthesis) using df.round(4). But zeros stay without any decimal places.

                parameter  pvalue   significance 4a
4   x1          0.1119     0.0257   **
4   x1_SE      (0.0502)     
3   x2          0.5996     0        ***
3   x2_SE      (0.1571)  

simply for optical reasons I'd like to have the same number of decimal places for zeros as well. The result should look like that:

                parameter  pvalue   significance 4a
4   x1          0.1119     0.0257   **
4   x1_SE      (0.0502)     
3   x2          0.5996     0.0000   ***
3   x2_SE      (0.1571)  

Upvotes: 1

Views: 3287

Answers (1)

ilyankou
ilyankou

Reputation: 1329

Update

You can set up your global pandas properties to display trailing zeros.

pd.options.display.float_format = '{:,.4f}'.format

Original

I find it tricky with trailing zeros. A workaround would be to create a column that has corresponding str values with trailing zeros.

df['pvalueFormatted'] = df['pvalue'].apply(lambda x: '{:,.4f}'.format(x))

Or if you want to save it as a .csv, you could use float_format:

df.to_csv('output.csv', float_format="%,.4f")

Upvotes: 3

Related Questions