好的我有以下工作程序。它打开的列数据文件对于excel来说太大了,并找到每列的平均值:
示例数据是:
Joe Sam Bob
1 2 3
2 1 3
它返回
Joe Sam Bob
1.5 1.5 3
这很好。问题是某些列将NA作为值。我想跳过这个NA并计算剩余值的平均值 所以
Bobby
1
NA
2
应输出为
Bobby
1.5
这是我现有的程序,在这里帮助建立。任何帮助表示赞赏!
with open('C://avy.txt', "rtU") as f:
columns = f.readline().strip().split(" ")
numRows = 0
sums = [0] * len(columns)
for line in f:
# Skip empty lines
if not line.strip():
continue
values = line.split(" ")
for i in xrange(len(values)):
sums[i] += int(values[i])
numRows += 1
with open('c://finished.txt', 'w') as ouf:
for index, summedRowValue in enumerate(sums):
print>>ouf, columns[index], 1.0 * summedRowValue / numRows
现在我有了这个:
以open('C://avy.txt',“rtU”)为f:
def get_averages(f):
headers = f.readline().split()
ncols = len(headers)
sumx0 = [0] * ncols
sumx1 = [0.0] * ncols
lino = 1
for line in f:
lino += 1
values = line.split()
for colindex, x in enumerate(values):
if colindex >= ncols:
print >> sys.stderr, "Extra data %r in row %d, column %d" %(x, lino, colindex+1)
continue
try:
value = float(x)
except ValueError:
continue
sumx0[colindex] += 1
sumx1[colindex] += value
print headers
print sumx1
print sumx0
averages = [
total / count if count else None
for total, count in zip(sumx1, sumx0)
]
print averages
它说:
追踪(最近一次通话): 文件“C:/avy10.py”,第11行,in lino + = 1 NameError:名称'lino'未定义
答案 0 :(得分:3)
这是一个功能性解决方案:
text = """Joe Sam Bob
1 2 3
2 1 3
NA 2 3
3 5 NA"""
def avg( lst ):
""" returns the average of a list """
return 1. * sum(lst)/len(lst)
# split that text
parts = [line.split() for line in text.splitlines()]
#remove the headers
names = parts.pop(0)
# zip(*m) does something like transpose a matrix :-)
columns = zip(*parts)
# convert to numbers and leave out the NA
numbers = [[int(x) for x in column if x != 'NA' ] for column in columns]
# all left is averaging
averages = [avg(col) for col in numbers]
# and printing
for name, x in zip( names, averages):
print name, x
我在这里写了很多列表推导,所以你可以打印出中间步骤,但那些可以是原因的生成器。
答案 1 :(得分:2)
[为清晰起见而编辑]
从文本文件中读取项目时,它们将作为字符串而非数字导入。这意味着如果您的文本文件具有数字3
并且您将其读入Python,则需要在执行算术运算之前将字符串转换为数字。
现在,您有一个包含colums的文本文件。每列都有一个标题和一组项目。每个项目都是数字或不是。如果它是一个数字,它将被函数float
正确转换,如果它不是有效数字(也就是说,如果转换不存在),转换将引发一个名为ValueError
的异常。
因此,您可以遍历列表和项目,因为它已在多个答案中正确解释。如果可以转换为float,则累积统计信息。如果没有,继续忽略该条目。
如果您需要更多关于什么是“鸭子打字”的信息(一种可以恢复为“更好地请求宽恕以获得许可”的范例),请查看Wikipedia link。如果你正在使用Python,你会经常听到这个术语。
下面我介绍一个可以累积统计数据的类(你对这个意思感兴趣)。您可以为表中的每一列使用该类的实例。
class Accumulator(object):
"""
Used to accumulate the arithmetic mean of a stream of
numbers. This implementation does not allow to remove items
already accumulated, but it could easily be modified to do
so. also, other statistics could be accumulated.
"""
def __init__(self):
# upon initialization, the numnber of items currently
# accumulated (_n) and the total sum of the items acumulated
# (_sum) are set to zero because nothing has been accumulated
# yet.
self._n = 0
self._sum = 0.0
def add(self, item):
# the 'add' is used to add an item to this accumulator
try:
# try to convert the item to a float. If you are
# successful, add the float to the current sum and
# increase the number of accumulated items
self._sum += float(item)
self._n += 1
except ValueError:
# if you fail to convert the item to a float, simply
# ignore the exception (pass on it and do nothing)
pass
@property
def mean(self):
# the property 'mean' returns the current mean accumulated in
# the object
if self._n > 0:
# if you have more than zero items accumulated, then return
# their artithmetic average
return self._sum / self._n
else:
# if you have no items accumulated, return None (you could
# also raise an exception)
return None
# using the object:
# Create an instance of the object "Accumulator"
my_accumulator = Accumulator()
print my_accumulator.mean
# prints None because there are no items accumulated
# add one (a number)
my_accumulator.add(1)
print my_accumulator.mean
# prints 1.0
# add two (a string - it will be converted to a float)
my_accumulator.add('2')
print my_accumulator.mean
# prints 1.5
# add a 'NA' (will be ignored because it cannot be converted to float)
my_accumulator.add('NA')
print my_accumulator.mean
# prints 1.5 (notice that it ignored the 'NA')
干杯。
答案 2 :(得分:-1)
将最内层循环更改为:
values = line.split(" ")
for i in xrange(len(values)):
if values[i] == "NA":
continue
sums[i] += int(values[i])
numRows += 1
答案 3 :(得分:-1)
更小的代码:
with open('in', "rtU") as f:
lines = [l for l in f if l.strip()]
names = '\t'.join(lines[0].split())
numbers = [[i.strip() for i in line.split()] for line in lines[1:]]
person_data = zip(*numbers)
person_data = [tuple(int(i) for i in t if i!="NA") for t in person_data]
averages = map(lambda x: str(float(sum(x))/len(x)), person_data)
with open('out', 'w') as f:
f.write(names)
f.write('\n')
f.write('\t'.join(averages))
我在John Machin发表评论后对此进行了测试。回应他的评论:
希望这更好。
答案 4 :(得分:-1)
以下代码正确处理不同的计数,并检测额外的数据......换句话说,它相当健壮。如果文件为空(2),如果标题行为空,则可以通过显式消息(1)来改进。另一种可能性是明确测试"NA"
,并在字段既不是"NA"
也不浮动时发出错误消息。
>>> import sys, StringIO
>>>
>>> data = """\
... Jim Joe Billy Bob
... 1 2 3 x
... 2 x x x 666
...
... 3 4 5 x
... """
>>>
>>> def get_averages(f):
... headers = f.readline().split()
... ncols = len(headers)
... sumx0 = [0] * ncols
... sumx1 = [0.0] * ncols
... lino = 1
... for line in f:
... lino += 1
... values = line.split()
... for colindex, x in enumerate(values):
... if colindex >= ncols:
... print >> sys.stderr, "Extra data %r in row %d, column %d" %
(x, lino, colindex+1)
... continue
... try:
... value = float(x)
... except ValueError:
... continue
... sumx0[colindex] += 1
... sumx1[colindex] += value
... print headers
... print sumx1
... print sumx0
... averages = [
... total / count if count else None
... for total, count in zip(sumx1, sumx0)
... ]
... print averages
修改在此处添加:
... return headers, averages
...
>>> sio = StringIO.StringIO(data)
>>> get_averages(sio)
Extra data '666' in row 3, column 5
['Jim', 'Joe', 'Billy', 'Bob']
[6.0, 6.0, 8.0, 0.0]
[3, 2, 2, 0]
[2.0, 3.0, 4.0, None]
>>>
修改强>
正常使用:
with open('myfile.text') as mf:
hdrs, avgs = get_averages(mf)