Click here to Skip to main content
15,916,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to get the mean, mode, standard deviation and median of a set of data from a text file. It works for all but the mode, when I include the mode I get the error of '
TypeError: unsupported format string passed to ModeResult.__format__
It goes when I comment out the print format line for the mode so I know thats whats wrong but I dont know why its wrong

What I have tried:

import numpy as np
from scipy import stats

def ColorText( text, color):
    Description: This function allows colors to be used in output formatting
    CEND = '\033[0m'

    CRED = '\033[1;31m'

    if color == 'red':
        return CRED + text + CEND  

data = np.loadtxt("sunspots.txt",float)
x = data[:,0]
y = data[:,1]

xyr = 1749 + (x/12)

title1= 'Mean'
title2= 'Median'
title3= 'Standard Deviation'
title4= 'Mode'

print(ColorText('\n''{0:>18}'.format(title1), 'red'),'|',\
ColorText('{0:>18}'.format(title2), 'red'), '|',\
ColorText('{0:>18}'.format(title3), 'red'),'|',\

mean = np.mean(y)
#print('The mean of the data sunspots is',mean)

median= np.median(y)
#print('The median of the data sunspots is', median)

standard_dev= np.std(y)
#print('The standard deviation of the data sunspots is',standard_dev)

mode= stats.mode(y)

print('{0:>18.3f}'.format(mean), '|',\
'{0:>18.3f}'.format(median), '|',\
Richard MacCutchan 12-Apr-20 13:22pm    
I do not think those > signs are valid.
CiaraMc96 12-Apr-20 14:01pm    
They work when I comment out the mode formatting line, Ive been using them for right justifying

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900