将公式的python列表写入excel

时间:2018-11-29 21:33:40

标签: python excel list formula

我的脚本将多个.csv文件读入数据帧,将所有文件添加到数据帧列表中,然后将它们合并到RecordKey上。

import pandas as pd
demoRaw = pd.read_csv("D2.csv", encoding = 'utf-8')
keyRaw = pd.read_csv("Key.csv", encoding = 'utf-8')
vitalRaw = pd.read_csv("V2.csv", encoding = 'utf-8')

dfs = [key, demo, vital, dx]

df_final = reduce(lambda left,right: pd.merge(left,right,on='Key'), dfs)

然后我添加一个名为Meds的空列

df_final["Meds"] = ""

创建Excel编写器

writer = pd.ExcelWriter('final.xlsx', engine='xlsxwriter')

分配df的长度并为公式创建空列表

length_of_df = len(df_final)
list_of_formulas = []

此循环(归功于W.Dodge)对公式进行迭代以匹配单元格

for i in range(2,length_of_df+2):
    formula = '=IF(COUNTIFS(Sheet2!A:A,A{0})>0,1,0)'.format(i)
    list_of_formulas.append(formula)

然后我尝试将列表写到Meds列

df_final.loc[:, "Meds"] = list_of_formulas

df_final.to_excel(writer, sheet_name='Combined')
meds.to_excel(writer, sheet_name='Meds')

writer.save()

但是,Excel返回以下错误消息:

enter image description here

选择否将返回一个空的工作簿。选择是返回...

enter image description here

其他所有内容都可以合并,但是Meds列填充为0,没有公式。

日志文件列表修复:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error133040_01.xml</logFileName><summary>Errors were detected in file 'C:\Users\4469177\Nate\Work\Coding\Python\scripts\final.xlsx'</summary><removedRecords summary="Following is a list of removed records:"><removedRecord>Removed Records: Formula from /xl/worksheets/sheet1.xml part</removedRecord></removedRecords></recoveryLog>

1 个答案:

答案 0 :(得分:1)

您的公式有误。您认为W.Dodge很明显是指this answer。但是请注意您的question中的公式:

x = keras.layers.concatenate([book_out, char_out], axis=1)

请注意会关闭COUNTIFS的括号。