Python:美化数字和dict输出
我正在做一个循环来输出并比较Python:美化数字和dict输出,python,formatting,ipython,ipython-notebook,jupyter-notebook,Python,Formatting,Ipython,Ipython Notebook,Jupyter Notebook,我正在做一个循环来输出并比较gof_列和gof_测试: pp.pprint(gof_train) pp.pprint(gof_test) 这会给我这样的结果(在IPython笔记本中): 它们很难被看到。我想知道是否有什么方法可以美化输出 具体来说,我想缩短数字,并使这两个dict的属性相互比较。大概是这样的: 'Chi_square': 2835.3674, 'Chi_square': 708.902, 'K_S': 0.050294, 'K_S': 0.0502, 'R_s
gof_列
和gof_测试
:
pp.pprint(gof_train)
pp.pprint(gof_test)
这会给我这样的结果(在IPython笔记本中):
它们很难被看到。我想知道是否有什么方法可以美化输出
具体来说,我想缩短数字,并使这两个dict的属性相互比较。大概是这样的:
'Chi_square': 2835.3674, 'Chi_square': 708.902,
'K_S': 0.050294, 'K_S': 0.0502,
'R_square': 0.8849, 'R_square': 0.8849
def compare_dicts(dict1, dict2, col_width):
print('{' + ' ' * (col_width-1) + '{')
for k1, v1 in dict1.items():
col1 = u" %s: %.3f," % (k1, v1)
padding = u' ' * (col_width - len(col1))
line = col1 + padding
if k1 in dict2:
line = u"%s %s: %.3f," % (line, k1, dict2[k1])
print(line)
print('}' + ' ' * (col_width-1) + '}')
dict1 = {
'foo': 0.43657,
'foobar': 1e6,
'bar': 0,
}
dict2 = {
'bar': 1,
'foo': 0.43657,
}
compare_dicts(dict1, dict2, 25)
我的想法
%精度
gof_train
的css
和gof_test
float:left
,那会很有趣,但我认为这是不可能的确实,您不能用CSS影响Python输出的显示,但是您可以将结果提供给一个格式化函数,该函数将负责使其“美观”。在您的情况下,您可以使用以下内容:
'Chi_square': 2835.3674, 'Chi_square': 708.902,
'K_S': 0.050294, 'K_S': 0.0502,
'R_square': 0.8849, 'R_square': 0.8849
def compare_dicts(dict1, dict2, col_width):
print('{' + ' ' * (col_width-1) + '{')
for k1, v1 in dict1.items():
col1 = u" %s: %.3f," % (k1, v1)
padding = u' ' * (col_width - len(col1))
line = col1 + padding
if k1 in dict2:
line = u"%s %s: %.3f," % (line, k1, dict2[k1])
print(line)
print('}' + ' ' * (col_width-1) + '}')
dict1 = {
'foo': 0.43657,
'foobar': 1e6,
'bar': 0,
}
dict2 = {
'bar': 1,
'foo': 0.43657,
}
compare_dicts(dict1, dict2, 25)
这使得:
{ {
foobar: 1000000.000,
foo: 0.437, foo: 0.437,
bar: 0.000, bar: 1.000,
} }
确实,您不能用CSS影响Python输出的显示,但是您可以将结果提供给一个格式化函数,该函数将负责使其“美观”。在您的情况下,您可以使用以下内容:
'Chi_square': 2835.3674, 'Chi_square': 708.902,
'K_S': 0.050294, 'K_S': 0.0502,
'R_square': 0.8849, 'R_square': 0.8849
def compare_dicts(dict1, dict2, col_width):
print('{' + ' ' * (col_width-1) + '{')
for k1, v1 in dict1.items():
col1 = u" %s: %.3f," % (k1, v1)
padding = u' ' * (col_width - len(col1))
line = col1 + padding
if k1 in dict2:
line = u"%s %s: %.3f," % (line, k1, dict2[k1])
print(line)
print('}' + ' ' * (col_width-1) + '}')
dict1 = {
'foo': 0.43657,
'foobar': 1e6,
'bar': 0,
}
dict2 = {
'bar': 1,
'foo': 0.43657,
}
compare_dicts(dict1, dict2, 25)
这使得:
{ {
foobar: 1000000.000,
foo: 0.437, foo: 0.437,
bar: 0.000, bar: 1.000,
} }
只需使用:
更改小数位数:
In [2]: pd.options.display.float_format = '{:.3f}'.format
制作一个数据帧:
In [3]: df = pd.DataFrame({'gof_test': gof_test, 'gof_train': gof_train})
并显示:
In [4]: df
Out [4]:
另一种选择是使用:
只需使用:
更改小数位数:
In [2]: pd.options.display.float_format = '{:.3f}'.format
制作一个数据帧:
In [3]: df = pd.DataFrame({'gof_test': gof_test, 'gof_train': gof_train})
并显示:
In [4]: df
Out [4]:
另一种选择是使用:
是对字符串格式的一个很好的解释是对字符串格式的一个很好的解释从来没有想过我可以使用
pandas
来实现这一点,这非常简洁!是的,pandas
确实又大又强大。我几乎每天都会发现一个新功能。没想到我可以用熊猫来做这个,这真是太棒了!是的,pandas
确实又大又强大。我几乎每天都会发现一个新功能。