0

After carrying a Chi2 test on some contingency table (using: scipy.stats.chi2_contingency(myTable)) I get some statistics:

stat=215.87297677691768
p=1.6223014100068392e-27
dof=36

I then print these results using the f-string formatting:

new_line = '\n'
print(
        f'Chi2 value = {stat:.4f}{new_line}'
        f'p-value = {p}{new_line}'
        f'Degrees of freedom = {dof}{new_line}'
    )

This gives me the output:

Chi2 value = 215.8730
p-value = 1.6223014100068392e-27
Degrees of freedom = 36

Is it possible to modify something in the p-value f-string in order to have the fractional coefficient of the p-value printed with 4 digits and so get the output:

Chi2 value = 215.8730
p-value = 1.6223e-27
Degrees of freedom = 36
Andrew
  • 926
  • 2
  • 17
  • 24
  • Does this answer your question? [round exponential float to 2 decimals](https://stackoverflow.com/questions/24118366/round-exponential-float-to-2-decimals) – Python learner Apr 13 '22 at 15:59
  • The link in the comment above already covers it. Simply change `{p}` to `{p:.5g}` – TomasO Apr 13 '22 at 16:22
  • Oh, I wasn't aware of the `g` possibility in `{**: .5g}` for example... Perfect! – Andrew Apr 13 '22 at 17:08

0 Answers0