Increment formula written to excel with Python

微笑、不失礼 提交于 2019-12-08 07:19:03

问题


The script below reads in multiple csv files, merges some and writes them to an Excel file in two different sheets.

It also adds this formula (=IF(COUNTIFS(Meds!A:A,B2)>0,1,0)) to the last column in every cell in the Meds column, but I need it to increment, so the second cell would be =IF(COUNTIFS(Meds!A:A,B3)>0,1,0) and so on. I can't figure out how to write a loop which will do this. I saw this post but I have issues using openpyxl, so would like to avoid that library.

import pandas as pd

# read in multiple csv files 
df1 = pd.read_csv("file1.csv", encoding = 'utf-8')
df2 = pd.read_csv("file2.csv", encoding = 'utf-8')
meds = pd.read_csv("meds.csv", encoding = 'utf-8')

# create a list of dataframes (excluding meds)
dfs = [df1, df2]

# merge dataframes in list
df_final = reduce(lambda left,right: pd.merge(left,right,on='RecordKey'), dfs)

# add empty column
df_final["Meds"] = ""

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('final.xlsx', engine='xlsxwriter')

# add formula to Meds
df_final['Meds'] = '=IF(COUNTIFS(Sheet2!A:A,E2)>0,1,0)'

# write to csv
df_final.to_excel(writer, sheet_name='Combined')
meds.to_excel(writer, sheet_name='Meds')

# Close the Pandas Excel writer and output the Excel file.
writer.save()

回答1:


You can use a loop and string formatting to create a list of formulas that can be inserted into your df.

length_of_df = len(df)
list_of_formulas = []

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

# print(list_of_formulas)

# ['=IF(COUNTIFS(Sheet2!A:A,E2>0,1,0)',
# '=IF(COUNTIFS(Sheet2!A:A,E3>0,1,0)',   
# '=IF(COUNTIFS(Sheet2!A:A,E4>0,1,0)',    
# '=IF(COUNTIFS(Sheet2!A:A,E5>0,1,0)', 
# '=IF(COUNTIFS(Sheet2!A:A,E6>0,1,0)']

# Assign list of formulas to df
df.loc[:, "Meds"] = list_of_formulas


来源:https://stackoverflow.com/questions/53509647/increment-formula-written-to-excel-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!