python-3.x - 如何在 python 中使用 xlsx writer 在现有 excel 中添加新列
问题描述
我需要在我现有的 Excel 工作表中添加一个新列,即 .xlsx,使用 Python 中的 Excel 编写器。
名称 |sub1 |sub2 |sub3。
内存。|10。|20。|30。
拉贾。| 11. | 22. | 33
我需要为 TOTAL 和 AVERAGE 添加新列,需要计算它并在 .xlsx 文件中显示输出。
需要在 Excel Writer 中做
解决方案
您可以将数据保存到xlsx
文件中并使用 pandas 进行如下计算:
import pandas as pd
df = pd.read_excel("test.xlsx")
total = df.sum(axis=1) #sums all cells in row
average = df.mean(axis=1) #averages all cells in row
df.insert(loc = 4 , column = "Total", value = total ) #inserting sum to dataframe
df.insert(loc = 5 , column = "Average", value = average ) #inserting avg to dataframe
writer = pd.ExcelWriter("test.xlsx")
df.to_excel(writer,"Sheet1") #Saving to df
writer.save()
你也可以用它df.to_excel("test.xlsx")
来缩短写作步骤
推荐阅读
- h2o - 具有交叉验证的 DRF 检查点失败并出现错误“ERRR:_weights_column:在训练帧中找不到权重列 '__internal_cv_weights__'”
- excel - 有没有办法做 IF +
- p5.js - 使用 RiTa 将数组排序为名词和动词
- python-3.x - 如何根据索引创建具有不同值集的新列?
- azure-devops - 在 Azure Devop 中触发 YAML 管道
- c# - Azure C#,过滤存储在 cosmos DB 中的原始数据(通过函数 URL 显示)
- python - 在 numpy 中使用布尔索引添加值的 Numba 同义词
- sorting - 如何对区分大小写的文本进行排序 - XSLT
- c++ - 在 CListCtrl 中搜索项目
- bots - Discord 机器人制造商 gif