1 回答
TA贡献1818条经验 获得超8个赞
您可以通过对单元格应用 Excel 条件格式来实现此目的,如下所示:
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data1': [10, 20, 30, 20, 15, 30, 45],
'Data2': [11, 20, 30, 21, 15, 31, 45]})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Add a format. Green fill with dark green text.
green_format = workbook.add_format({'bg_color': '#C6EFCE',
'font_color': '#006100'})
# Apply a conditional format to the cell range.
worksheet.conditional_format(1, 2, 7, 2, {'type': 'cell',
'criteria': 'equal to',
'value': '=$B2',
'format': green_format})
# Close the Pandas Excel writer and output the Excel file.
writer.save()
输出:
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报