Python:如何访问返回值以放入表中?



注意:我是一个非常初学者,所以请耐心等待!

编辑:我已经修复了错误,但我需要帮助解决下面的问题!

我的问题是:

1(如果我想将最小值和最常见的单词/数字放在表格上,我怎样才能索引到最小值/最常见的单词并将其提取并放置在表格上的正确位置?

描述

下面的代码应该使用函数转置给定的嵌套列表 A。

def rows2cols(A):

然后运行该列表,对于每一列,我检查它是否有数值或不使用

def isnumericlist(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']]
minVal = []
maxVal = []
hist = []
average = []
stanDev = []
headers = A[0] #this sets the variable "headers" as the first row 
rows = A[1:] #skips the first row
def rows2cols(A):
if len(A) == 0: 
return []                      #this covers the base case of having an empty csv file
res  = [[] for x in headers]       # would create 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):
res = B[0] #first column AFTER you transpose the nested list
for x in A:
if x < res:
res = x
return res
def most_common(A):
counts = {}
for x in A:
counts[tuple(x)] = counts.get(tuple(x), 0) + 1 
max = -1
maxKey = ""
for key,value in counts.items():
if max < value:
max = value
maxKey = key
return maxKey
def notnumeric(A):
return "n/a"
cols = rows2cols(rows)
for col in range(len(headers)):
if isnumericlist(cols[col]):
B = convertstringtofloats(cols[col])
minVal.append(getMin(B))
maxVal.append(getMax(B))
average.append(getAvg(B))
stanDev.append(getSD(B))
else:
notnumeric(col)
mode.append(most_common(cols[col]))
tablevalues = [minVal, maxVal, average, stanDev, mode]

我生成表的代码如下,以及一个示例表,说明我希望结果如何!

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
table = [
["Columns:", "Min", "Max", "Avg", "Std. Dev.", "Most Common Word"],
["OrderDate", "n/a", "n/a", "n/a", "n/a", "John"],
["Region", 3.3, 6.29, 4.888, 1.333, 4.911],
["Rep", 1.3, 3.2, 1.888, 0.333, 1.9],
["Item", 1.3, 3.2, 1.888, 0.333, 1.9],
["Units","n/a", "n/a", "n/a", "n/a", "John"],
["Unit Price","n/a", "n/a", "n/a", "n/a", "John"]
]
print_table(table)

可能熊猫可能对你有帮助。df.describe(include='all')将为您提供所需的桌子。您只需要使用熊猫读取数据 A,并根据需要更改每列中的数据类型。top是相应列中最常见的单词,freq是该特定单词的出现时间。您甚至可以将此表另存为新数据帧,df2 = df.describe(include='all')

import pandas as pd
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']]
df = pd.DataFrame(A[1:],columns=A[0])
print(df)
OrderDate   Region  Rep Item    Units   Unit Price
0   04-Jul-2014 East    Richard Pen Set 62  4.99
1   12-Jul-2014 East    Nick    Binder  29  1.99
2   21-Jul-2014 Central Morgan  Pen Set 55  12.49
3   29-Jul-2014 East    Susan   Binder  81  19.99
4   07-Aug-2014 Central Matthew Pen Set 42  23.95
5   15-Aug-2014 East    Richard Pencil  35  4.99
6   24-Aug-2014 West    James   Desk    3   275.00
df = df.astype(dtype={'OrderDate':'str', 'Region':'str',
'Rep':'str', 'Item':'str', 'Units':'int', 'Unit Price':'float'})
df['OrderDate'] = df.OrderDate.apply(
lambda x: pd.to_datetime(x).strftime('%d-%b-%Y'))
print(df.dtypes)
OrderDate      object
Region         object
Rep            object
Item           object
Units           int32
Unit Price    float64
dtype: object
print(df.describe(include='all'))
OrderDate   Region  Rep Item    Units   Unit Price
count   7   7   7   7   7.000000    7.000000
unique  7   3   6   4   NaN NaN
top     24-Aug-2014 East    Richard Pen Set NaN NaN
freq    1   4   2   3   NaN         NaN
mean    NaN NaN NaN NaN 43.857143   49.057143
std     NaN NaN NaN NaN 25.182193   99.968112
min     NaN NaN NaN NaN 3.000000    1.990000
25%     NaN NaN NaN NaN 32.000000   4.990000
50%     NaN NaN NaN NaN 42.000000   12.490000
75%     NaN NaN NaN NaN 58.500000   21.970000
max     NaN NaN NaN NaN 81.000000   275.000000

最新更新