pandas 用熊猫为excel中的单元格着色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39299509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:57:03  来源:igfitidea点击:

coloring cells in excel with pandas

pythonexcelpandasduplicateshighlight

提问by Carlos Arronte Bello

I need some help here. So i have something like this

我需要一些帮助。所以我有这样的事情

import pandas as pd
path = '/Users/arronteb/Desktop/excel/ejemplo.xlsx'
xlsx = pd.ExcelFile(path)
df = pd.read_excel(xlsx,'Sheet1')
df['is_duplicated'] = df.duplicated('#CSR')
df_nodup = df.loc[df['is_duplicated'] == False]
df_nodup.to_excel('ejemplo.xlsx', encoding='utf-8')

So basically this program load the ejemplo.xlsx(ejemplo is example in Spanish, just the name of the file) into df(a DataFrame), then checks for duplicate values in a specific column??. It deletes the duplicates and saves the file again. That part works correctly. The problem is that instead of removing duplicates, I need highlight the cells containing them with a different color, like yellow.

所以基本上这个程序将ejemplo.xlsx(ejemplo 是西班牙语的例子,只是文件名)加载到df(a DataFrame) 中,然后检查特定列中的重复值??。它会删除重复项并再次保存文件。那部分工作正常。问题是,我需要用不同的颜色(如黄色)突出显示包含它们的单元格,而不是删除重复项。

回答by Harrison

You can create a function to do the highlighting...

您可以创建一个函数来突出显示...

def highlight_cells():
    # provide your criteria for highlighting the cells here
    return ['background-color: yellow']

And then apply your highlighting function to your dataframe...

然后将您的突出显示功能应用于您的数据框...

df.style.apply(highlight_cells)

回答by M T Head

I just had this same problem and I just solved it this week. My problem was not getting the includes to work properly to get the online code that I found working properly.

我刚刚遇到了同样的问题,本周我刚刚解决了它。我的问题是没有让包含正常工作以获取我发现正常工作的在线代码。

I am going to assume you mean change the background color not change the font color. If I am wrong clarify your request.

我假设您的意思是更改背景颜色而不是更改字体颜色。如果我错了,请澄清您的要求。

My solution is tied to a particular library. openpyxl

我的解决方案与特定库相关联。打开pyxl

#### This import section is where my mistake was at
#### This works for me
import openpyxl    ### Excel files 
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from openpyxl.styles import Fill, Color
from openpyxl.styles import Style
from openpyxl.styles.colors import RED
from openpyxl.styles.colors import GREEN


str_xls_PathFileCurrent = str_xls_FileName
### Opens Excel Document
var_xls_FileOpen    = openpyxl.load_workbook(str_xls_PathFileCurrent) 
### Opens up the Excel worksheet 
var_xls_TabName     = var_xls_FileOpen.worksheets[0]                  
### Put the spreadsheet tab names into an array 
ary_xls_SheetNames  = var_xls_FileOpen.get_sheet_names()              
### Open the sheet in the file you working on 
var_xls_TabSheet    = var_xls_FileOpen.get_sheet_by_name(ary_xls_SheetNames[0])
xls_cell = var_xls_TabSheet['d10']

#### Changes the cell background color 
xls_cell.style = Style(fill=PatternFill(patternType='solid'
    , fgColor=Color('C4C4C4')))  ### Changes background color 

#### Changes the fonts (does not use style) 
xls_cell.font = xls_cell.font.copy(color  = 'FFFF0000') ### Works (Changes to red font text) 
xls_cell.font = xls_cell.font.copy(bold  = True) ### Works (Changes to bold font) 
xls_cell.font = xls_cell.font.copy(italic= True) ### Works (Changes to Italic Text) 
xls_cell.font = xls_cell.font.copy(size  =   34) ### Works (Changes Size)