pandas 属性错误:尝试将 Dataframe 追加到现有Excel工作表时,"MergedCell"对象属性"value"为只读

jxct1oxe  于 2022-12-21  发布在  其他
关注(0)|答案(1)|浏览(1178)

我试图将我的数据框附加到现有的Excel工作表中,我选择了ExcelWriter覆盖路径。以下是我的完整应用程序代码:

import pandas as pd
from openpyxl import load_workbook

# import excel file and create dataframe
poam = pd.read_excel('finalpoam.xlsm', header = 4)
new_poam = poam[["POAM ID", "Weakness Source Identifier", "Asset Identifier", "Weakness Name", "Weakness Detector Source", "Original Risk Rating", "Original Detection Date", "Weakness Description", "Overall Remediation Plan", "False Positive"]]

# variables
oracle = "oracle mysql"
sev = "moderate"
og_date = "2022-11"
false = "No"
filename = "finaldr.xlsx"

# alter dataframe
oracle_df = new_poam[new_poam['Weakness Name'].str.contains(oracle, case=False, na=False)]
moderate_df = oracle_df[oracle_df['Original Risk Rating'].str.contains(sev, case=False, na=False)]
false_df = moderate_df[moderate_df['False Positive'].str.contains(false, case=False, na=False)]
final_df = false_df.drop(labels=range(783, 788), axis=0)
final_df = final_df.drop(columns="False Positive")

# append dataframe to existing excel sheet "DR Sheet" in the "finaldr.xlsx" file
with pd.ExcelWriter(filename, mode="a", engine="openpyxl", if_sheet_exists="overlay") as writer:
    final_df.to_excel(writer, sheet_name="DR Sheet", header=None)

这样做会导致此错误

AttributeError: 'MergedCell' object attribute 'value' is read-only

我试着浏览其他帖子,解决方案让我很困惑-我看到一些解决方案提到一个特定的单元格作为“合并单元格”,但我不知道什么是合并单元格,也不知道如何找到它

cpjpxq1n

cpjpxq1n1#

以防万一有人看到这个,幸运的是我的修复相当简单:我所要做的就是指定起始行(附加DataFrame的起始行),这样就解决了这个错误。下面是我的代码和相应的解决方案,startrow=0参数被添加到.to_excel()函数中:

    • 解决方案**
with pd.ExcelWriter(filename, mode="a", engine="openpyxl", if_sheet_exists="overlay") as writer:
    final_df.to_excel(writer, sheet_name="DR Sheet", startrow=456, header=None)

以下是有关**.to_excel**函数和其他可用输入的更多信息。

相关问题