【问题标题】:How can I insert an excel formula using pandas when I save it as an excel file?当我将其保存为 excel 文件时,如何使用 pandas 插入 excel 公式?
【发布时间】:2020-09-18 15:58:07
【问题描述】:

我有以下具有多个列和行的数据框,

A  |  B  |  C  |  D     |  E   |....
2  |  b  |  c  |  NaN   |  1   |
3  |  c  |  b  |  NaN   |  0   |
4  |  b  |  b  |  NaN   |  1   |
.
.
.

有没有办法通过在输出 excel 文件中使用 python 的示例以下面所述的方式添加 excel 公式(对于某些列)?

例如,我希望能够有这样的输出,

  =SUM(A0:A2)   |     |     |                             | =SUM(E0:E2)
       A        |  B  |  C  |              D              |      E
0      2        |  b  |  c  |  =IF(B0=C0, "Yes", "No")    |      1
1      3        |  c  |  b  |  =IF(B1=C1, "Yes", "No")    |      0
2      4        |  b  |  b  |  =IF(B2=C2, "Yes", "No")    |      1
.      
.
.

最终输出,

       9        |     |     |         |      2
       A        |  B  |  C  |  D      |      E
0      2        |  b  |  c  |  No     |      1
1      3        |  c  |  b  |  No     |      0
2      4        |  b  |  b  |  Yes    |      1
.      
.
.

我想在最终输出的 excel 文件中添加公式,以便如果列的值(在最终输出的 excel 文件中)有任何变化,也可以在 excel 文件中实时更新其他列,例如,

       15       |     |     |         |      3
       A        |  B  |  C  |  D      |      E
0      2        |  b  |  b  |  Yes    |      1
1      9        |  c  |  b  |  No     |      1
2      4        |  b  |  b  |  Yes    |      1
.      
.
.

如果我将 A1 的值从 3 更改为 9,那么列的总和将更改为 15;当我将C0的值从“c”变为“b”时,其对应的行值的值,即D0从“No”变为“Yes”; Col E 也一样。

我知道您可以使用 xlsxwriter 库来编写公式,但我无法弄清楚如何按照上面示例中所述的方式添加公式。

任何帮助将不胜感激,在此先感谢!

【问题讨论】:

  • 如果您想保留 Excel 公式,您可能需要使用 openpyxl

标签: python excel pandas


【解决方案1】:

您最好通过xlsxwriter 而不是pandas 完成您希望保留的所有公式。

如果您只想导出结果,可以使用pandas,因为您想保留公式,请在编写电子表格时执行。

下面的代码会将dataframe 和公式写到名为testxlsx 文件中。

import xlsxwriter
import pandas as pd

from numpy import nan

data = [[2, 'b', 'c', nan, 1], [3, 'c', 'b', nan, 0], [4, 'b', 'b', nan, 1]]
df = pd.DataFrame(data=data, columns=['A', 'B', 'C', 'D', 'E'])


## Send values to a list so we can iterate over to allow for row:column matching in formula ##

values = df.values.tolist()

## Create Workbook ##

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

row = 0
col = 0

## Iterate over the data we extracted from the DF, generating our cell formula for 'D' each iteration ##

for idx, line in enumerate(values):
    d = f'=IF(B{row + 1}=C{row + 1}, "Yes", "No")'
    a, b, c, _, e = line
   
    ## Write cells into spreadsheet ##

    worksheet.write(row, col, a)
    worksheet.write(row, col + 1, b)
    worksheet.write(row, col + 2, c)
    worksheet.write(row, col + 3, d)
    worksheet.write(row, col + 4, e)
    
    row += 1

## Write the total sums to the bottom row of the sheet utilising the row counter to specify our stop point ##        
worksheet.write(row, 0, f'=SUM(A1:A{row})')
worksheet.write(row, 4, f'=SUM(E1:E{row})')

workbook.close()

【讨论】:

  • 感谢您的回答。有没有办法在标题行的顶部而不是底部打印总和?
  • 当然,只需编辑行/列字段以将其放置在您想要的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多