Pandas 朱庇特公司;熊猫-当Dataframe为空且仅列标题时更改选项卡颜色?

Pandas 朱庇特公司;熊猫-当Dataframe为空且仅列标题时更改选项卡颜色?,pandas,jupyter-notebook,jupyter,Pandas,Jupyter Notebook,Jupyter,我有一个Jupyter笔记本,它运行多个数据帧来查找问题的数据。我将我的每个数据框放在excel工作表的一个单独选项卡上,因此每个工作表要么有数据,要么只有列标题 我想知道的是,当特定数据框中没有数据时(当选项卡只有列标题时),如何将选项卡颜色更改为红色 我正在使用openpyxl打开和保存当前工作表: writer = pd.ExcelWriter(out_path, engine='openpyxl') df.to_excel(writer, sheet_name='TEST') write

我有一个Jupyter笔记本,它运行多个数据帧来查找问题的数据。我将我的每个数据框放在excel工作表的一个单独选项卡上,因此每个工作表要么有数据,要么只有列标题

我想知道的是,当特定数据框中没有数据时(当选项卡只有列标题时),如何将选项卡颜色更改为红色

我正在使用openpyxl打开和保存当前工作表:

writer = pd.ExcelWriter(out_path, engine='openpyxl')
df.to_excel(writer, sheet_name='TEST')
writer.save()

可能吗

试试XlsxWriter

从xlsxwriter.工作簿导入工作簿

workbook = Workbook('worksheets.xlsx')

# Put together the worksheets.
WT1 = workbook.add_worksheet()
WT2 = workbook.add_worksheet()

# Now you can set the colors of the tabs
WT1.set_tab_color('blue')
WT2.set_tab_color('red')
那么:

# Check if DF only has headers and add code that you need to change color based on that condition

d = {'col1': [], 'col2': []}
df = pd.DataFrame(data=d)
df

if len(df.index) == 0:
    WT2.set_tab_color('red')

在if语句中,您应该能够使用XlsxWriter仅更改颜色选项卡。

我没有使用XlsxWriter,因为我使用的数据帧具有多个选项卡和数据帧,我了解到您不能使用XlsxWriter。从您的示例中看,您已经在使用XlsxWriter了,您可以继续使用它来操作excel,并为其他DF操作保留命令。见我的第二个建议