我正在尝试向表中输入值,但是表并没有按照我想要的方式输出。给定的csv文件的标题(“ OrderDate”,“ Rep”等)应位于下图的“列:”单元格下面:Table of statistical values
我尝试创建可以转置标题的多个函数,但是当尝试打印表时,会出现错误:
TypeError: unsupported format string passed to list.__format__.
在“标签”行之前,我讨厌输入一个代码:
headers2 = [x.split() for x in headers]
P.S。我已经删除了csv文件代码,并手动将其放入分配给“ A”的列表中。
我的代码:
A = [['OrderDate', 'Region', 'Rep', 'Item', 'Units', 'Unit Price'],
['4-Jul-2014', 'East', 'Richard', 'Pen Set', '62', '4.99'],
['12-Jul-2014', 'East', 'Nick', 'Binder', '29', '1.99'],
['21-Jul-2014', 'Central', 'Morgan', 'Pen Set', '55', '12.49'],
['29-Jul-2014', 'East', 'Susan', 'Binder', '81', '19.99'],
['7-Aug-2014', 'Central', 'Matthew', 'Pen Set', '42', '23.95'],
['15-Aug-2014', 'East', 'Richard', 'Pencil', '35', '4.99'],
['24-Aug-2014', 'West', 'James', 'Desk', '3', '275'],
['1-Sep-2014', 'Central', 'Smith', 'Desk', '2', '125']]
minVal = []
maxVal = []
hist = []
average = []
stanDev = []
mode = []
headers = A[0] #this sets the variable "headers" as the first row
rows = A[1:] #sets the variable 'rows' to be a nested list without headers
def rows2cols(A):
if len(A) == 0: #this covers the base case of having an empty csv file
return []
res = [[] for x in headers] #creates a list of empty lists
for line in A:
for col in range(len(line)):
res[col].append(line[col])
return res
def convertstringtofloats(A):
res = []
for x in A:
res.append(float(x))
return res
def isnumericlist(A):
for x in A:
try:
numeric = float(x)
except:
return False
return True
def getMin(A):
B = convertstringtofloats(cols[col]) #Let's Python know what B is for the next line. If this isn't here, there is an error.
res = B[0]
for x in A:
if x < res:
res = x
return res
def getMax(A):
B = convertstringtofloats(cols[col]) #Let's Python know what B is for the next line. If this isn't here, there is an error.
res = B[0]
for x in A:
if x > res:
res = x
return res
def getAvg(A):
return sum(A)/len(A)
def most_common(A):
counts = {}
for x in A:
counts[(x)] = counts.get((x), 0) + 1
max = -1
maxKey = ""
for key,value in counts.items():
if max < value:
max = value
maxKey = key
return maxKey
def getSD(A):
sumsq = 0
for n in A:
sumsq += (getAvg(A))**2
return sumsq
cols = rows2cols(rows) #transposes 'rows' and assigns to variable 'cols'
def stats(A):
B = convertstringtofloats(A)
minVal.append(getMin(B))
maxVal.append(getMax(B))
average.append(getAvg(B))
stanDev.append(getSD(B))
for col in range(len(headers)):
if isnumericlist(cols[col]):
stats(cols[col]) #calls the function to calculate stats of the transposed matrix
else:
minVal.append("n/a")
maxVal.append("n/a")
average.append("n/a")
stanDev.append("n/a")
mode.append(most_common(cols[col]))
#headers2 = [x.split() for x in headers]
labels = ["Columns:", "Min", "Max", "Avg", "Std. Dev.", "Most Common Word"] #labels for the table
table_values = [labels, headers, minVal, maxVal, average, stanDev, mode] #combines all the calculated stats into a single list
print(table_values)
def print_table(table):
longest_cols = [
(max([len(str(row[i])) for row in table]) + 0) for i in range(len(table[0]))
]
row_format = "|".join([" {:>" + str(longest_col) + "} " for longest_col in longest_cols])
first = True
for row in table:
print(row_format.format(*row))
if first:
print((sum(longest_cols) + (len(table[0]) - 0) * 3) * "-")
first = False
print_table(table_values) # this prints the 'labels' at the top, but the statistical values are not in the right place