[英]How to average across excel rows for multiple rows using Openpyxl in Python?
I am currently trying to average 3 excel columns(Col C to E) into and new 4th column (Col F).我目前正在尝试将 3 个 excel 列(Col C 到 E)平均到新的第 4 列(Col F)。 When I look up how to do this with openpyxl the code looks like this:
当我查找如何使用 openpyxl 执行此操作时,代码如下所示:
from openpyxl import load_workbook
wb = load_workbook('PythontestAvg.xlsx')
sheet1 = wb['full trace']
sheet2 = wb['full trace Copy']
sheet2["F1"] = '=AVERAGE(C1:E1)'
sheet2["F2"] = '=AVERAGE(C2:E2)'
However, doing this is very tedious for 1500 cells from F1 to F1501.但是,对于从 F1 到 F1501 的 1500 个单元,这样做非常繁琐。 Is there a way to write a code that will let me average each of the rows for the 3 columns and paste in Column F?
有没有办法编写一个代码,让我平均 3 列的每一行并粘贴到 F 列? Basically a code that will give the same result as:
基本上一个代码将给出相同的结果:
sheet2["F1"] = '=AVERAGE(C1:E1)'
sheet2["F2"] = '=AVERAGE(C2:E2)'
sheet2["F3"] = '=AVERAGE(C3:E3)'
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
sheet2["F1501"] = '=AVERAGE(C1501:E1501)'
Please let me know if any more clarification is needed.如果需要进一步澄清,请告诉我。 Thanks in advance
提前致谢
Seems like you are looking for a for loop similar to this:似乎您正在寻找与此类似的 for 循环:
for i in range(1, 1502):
sheet2["F" + str(i)] = '=AVERAGE(C%s:E%s)' % (i,i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.