用python在Excel中计算值

我是Python新手,在这个问题上遇到很多麻烦,这是我必须要做的工作。

有关excel文件的一些背景:有3列,约100行。 第一列(col1)包含A或B.第二列(col2)包含范围从1到10的任何数字。第三列(col3)包含任何十进制数的值。

我想要的程序是通过数据parsing。 会有很多col1和col2的重复。 例如,(A,1)可以在行1,5,20,98等上,但是col3将是不同的数字。 所以对于第三栏的不同数字,我想要find所有这些数字的平均值。

输出应该是这样的:

A, 1 = avg 4.32 A, 2 = avg 7.23 A, 3 = avg -9.12 etc etc (until number 10) B, 1 = avg 3.76 B, 2 = avg -8.12 B, 3 = avg 1.56 etc etc (until number 10) 

它不一定是完整的字母和数字顺序,它可以打印出它find的第一个组合。但是我已经在我的代码中做到了这一点,出于某种原因,它不打印出所有组合,只有3。

 import xlrd #import package #opening workbook and reading first sheet book = xlrd.open_workbook('trend.xls') sheet = book.sheet_by_index(0) #function to hold unique combos unique_combinations = {} #looping through data for row_index in range(sheet.nrows): #declaring what group equals to what row col1 = sheet.cell(row_index, 0) col2 = sheet.cell(row_index, 1) col3 = sheet.cell(row_index, 2) unique_combo = (col1.value, col2.value) if unique_combinations.has_key(unique_combo): unique_combinations[unique_combo].append(col3.value) else: unique_combinations[unique_combo] = [col3.value] for k in unique_combinations.keys(): l = unique_combinations[k] average = sum(l) / len(l) print '%s: %s Mean = %s' % (k[0], k[1], average) 

从本质上来说,它基本上是2个组,在2个组内是另外10个组,在这10个组内是这些数字的平均值。

请帮忙! 非常感谢你提前。

EXCEL文件示例:

 col1 | col2 | col3 A | 1 | 3.12 B | 9 | 4.12 B | 2 | 2.43 A | 1 | 9.54 B | 8 | 2.43 A | 2 | 1.08 

那么程序要做的是看到它遇到的第一个组合是A,1,它将把3.12存储在一个列表中,然后看下一个并继续存储,直到遇到一个重复的是第四个行。 它也会存储这个值。 在结尾处,输出将显示A,1 = avg(3.12 + 9.54 / 2)。 这个例子只显示A,1组合。 但实际上,只有2个组(例如),但是col2的范围可以是1到10。

这个build议更多的是“如何解决正在发生的事情”,而且比评论更容易阅读答案。

我认为值得添加debugging打印和exception处理。

我用OpenOffice和Python 2.7尝试了这个示例。 如果在最后一个循环中发生exception,并且在testing运行中吞食stderr,我可以重现您的症状。 例如: python test.py 2>nul

所以我build议你试试这个:

 import xlrd book = xlrd.open_workbook('trend.xls') sheet = book.sheet_by_index(0) unique_combinations = {} for row_index in range(sheet.nrows): col1 = sheet.cell(row_index, 0) col2 = sheet.cell(row_index, 1) col3 = sheet.cell(row_index, 2) unique_combo = (col1.value, col2.value) if unique_combinations.has_key(unique_combo): print 'Update: %r = %r' % (unique_combo, col3.value) unique_combinations[unique_combo].append(col3.value) else: print 'Add: %r = %r' % (unique_combo, col3.value) unique_combinations[unique_combo] = [col3.value] for k in unique_combinations.keys(): l = unique_combinations[k] try: average = sum(l) / len(l) print '%s: %s Mean = %s' % (k[0], k[1], average) except Exception, e: print 'Ignoring entry[%r]==%r due to exception %r' % (k, l, e) 

这应该可以帮助你判断你的“怪异行为”。

给大pandas一试:

 In [1]: import pandas as pd In [2]: xls = pd.ExcelFile('test.xls') ...: df = xls.parse('Sheet1', header=None) ...: In [3]: df Out[3]: 0 1 2 0 A 1 3.12 1 B 9 4.12 2 B 2 2.43 3 A 1 9.54 4 B 8 2.43 5 A 2 1.08 In [4]: groups = df.groupby([0,1]) In [5]: for k, g in groups: ...: print k, g[2].mean() ...: (u'A', 1.0) 6.33 # your example (3.12 + 9.54) / 2 (u'A', 2.0) 1.08 (u'B', 2.0) 2.43 (u'B', 8.0) 2.43 (u'B', 9.0) 4.12 

如果你想要所有的手段作为一个列表,完整的脚本将是:

 import pandas as pd df = pd.ExcelFile('test.xls').parse('Sheet1', header=None) print [g[2].mean() for _, g in df.groupby([0,1])] # out: [6.3300000000000001, 1.0800000000000001, 2.4300000000000002, 2.4300000000000002, 4.1200000000000001]