【问题标题】:How add new colum in existing excel using xlsx writer in python如何在 python 中使用 xlsxwriter 在现有 excel 中添加新列
【发布时间】:2021-04-02 10:44:28
【问题描述】:

我需要在我现有的 Excel 工作表中添加一个新列,即 .xlsx,使用 Python 中的 Excel 编写器。

名称 |sub1 |sub2 |sub3。
内存。 |10。 |20。 |30.
拉贾。| 11. | 22. | 33

我需要为 TOTAL 和 AVERAGE 添加新列,并且需要计算它并在 .xlsx 文件中显示输出。

需要在 Excel Writer 中做

【问题讨论】:

    标签: python-3.x openxlsx pandas.excelwriter spreadsheet-excel-writer


    【解决方案1】:

    您可以将数据保存到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")来缩短写作步骤

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-18
      • 2017-12-19
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 2020-07-23
      相关资源
      最近更新 更多