Formatting Test Statistics and P-values in Python
tags: #python
def get_significance(p):
"""Returns the significance of a p-values as a string of stars."""
if p <= 0.001:
return '***'
elif p <= 0.01:
return '**'
elif p <= 0.05:
return '*'
elif p <= 0.1:
return '.'
else:
return ''
def round_p_value(p):
"""Round a small p-value so that it is human-readable."""
if p < 0.001:
return '<0.001'
else:
return f'{p:5.3}'
p_rounded = round_p_value(pvalue)
significance = get_significance(pvalue)
print(f'The p-value is {p_rounded} ({significance})')