Python:如何访问返回值以放入表中?
Posted
技术标签:
【中文标题】Python:如何访问返回值以放入表中?【英文标题】:Python: How to access returned values to put into a table? 【发布时间】:2019-03-10 08:59:21 【问题描述】:注意:我是 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)
【问题讨论】:
你能把你的问题分解成哪一行出现错误吗?您是否希望读者首先阅读您的整个代码,然后逐行找出错误可能来自哪里?此外,most_common(cols)
只是调用该函数,但您没有将返回值存储在任何变量中
您的错误发生在哪里对我来说并不明显。您可以编辑问题以包含异常的完整回溯吗?你已经
@Bazingaa,@Blckknght;对不起!我在尝试修复我的错误时创建了这个问题,最终我自己修复了它。我只需要代码下方的问题帮助。
@Bazingaa 如何存储返回值?此外,当我将代码放入 PythonTutor 网站时,“most_common(cols)”不会遍历每个 cols 中的每个元素。它似乎会跳过每个列表,直到它到达带有数值的列表。
【参考方案1】:
Pandas 可能会对您有所帮助。 df.describe(include='all')
将为您提供所需的桌子。您只需要使用 pandas 读取数据 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
【讨论】:
您能否提供一个解决方案,说明如何在没有 Pandas 的情况下做到这一点?我被允许使用的唯一导入是“csv”和“matplotlib.pyplot”。感谢您的意见! @K.Wong 使用嵌套 for 循环是可行的,但麻烦且时间效率低。如果不是熊猫,你至少需要 numpy。但是,强烈建议将后者用于财务和计量经济学分析。对不起,如果答案没有帮助。以上是关于Python:如何访问返回值以放入表中?的主要内容,如果未能解决你的问题,请参考以下文章
如何构造值以使所有视图都可以在 Swift/SwiftUI 中访问其值?
python 格式化单个值以在符合RFC4180的CSV文件中使用。使用逗号将这个函数返回的多个值连接到crea