简体   繁体   English

如何在 Python 中使用 Openpyxl 对多行的 excel 行进行平均?

[英]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.

相关问题 如何使用 Python 将一个 excel 拆分为多个 excel,并在所有新 excel 中分配相同的行数? - How to split one excel into multiple excel with common number of rows distribution across all the new excel using Python? openpyxl在Excel中写多行 - openpyxl write multiple rows in Excel 使用 python 中的 openpyxl 将特定行和列添加到另一个 excel 文件中 - add specific rows and column into another excel file using openpyxl in python 如果满足条件,如何使用 openpyxl python 删除 excel 中的特定行 - How to delete specific rows in excel with openpyxl python if condition is met 如何使用Python和openpyxl复制Excel行并过滤值? - How can I copy Excel rows, filtering for a value, with Python and openpyxl? 如何使用openpyxl和python3为excel工作表中的一系列单元格(列和行)提供字体颜色? - How to give font color to a range of cells (columns and rows) in excel worksheet using openpyxl and python3? 如何在python中使用openpyxl将excel文件的行移动到第1行并使用UTF-8编码转换为csv? - How to move rows of excel file to line 1 and convert to csv with UTF-8 encoding using openpyxl in python? 如果使用 python openpyxl 在特定列中的单元格为空白,如何删除 Excel 中的行? - How to delete rows in Excel if cells in specific column are blank using python openpyxl? 如何使用 OpenPyXL 遍历 Excel 表中的所有行? - How to iterate over all rows in an Excel Table using OpenPyXL? 如何使用 openpyxl 遍历 Excel 表的行? - How to loop through rows of the Excel sheet using openpyxl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM