[英]Pandas: Setting decimal places for zeros
I have a dataframe showing regression output.我有一个 dataframe 显示回归 output。 I want to style it for publication.
我想为它设计样式以供发布。 I rounded all values (before putting them between parenthesis) using
df.round(4)
.我使用
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)
Update更新
You can set up your global pandas properties to display trailing zeros.您可以设置全局 pandas 属性以显示尾随零。
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.一种解决方法是创建一个列,该列具有相应的
str
值和尾随零。
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:或者如果你想将它保存为a.csv,你可以使用float_format:
df.to_csv('output.csv', float_format="%,.4f")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.