[英]Filter on conditional formatting in excel sheet using pandas
I have an excel file that has conditional formatting with red color in certain rows one of the columns.我有一个 excel 文件,该文件在列的某些行中具有红色的条件格式。 So my file looks like this
所以我的文件看起来像这样
Now I have to apply the filter on the "College" column to delete all the rows that have a red-colored background.现在我必须在“College”列上应用过滤器以删除所有具有红色背景的行。
And save it back to the file.并将其保存回文件。
The code I wrote for this is:我为此编写的代码是:
dataFrame_file = pd.read_excel(util.comcastFile2Path(), sheet_name='Sheet1') //comcastFile2Path() gives path of file
def only_cells_with_red_background(cell):
return cell if cell.style.bg_color in {utils.colors.red, 'FFFF0000'} else np.nan
df=dataFrame_file.style.applymap(only_cells_with_red_background,subset=['College'])
util.mergeFile(dataFrame_file,df,util.comcastFile2Path)
And my util class method for merging and saving file looks like this我的用于合并和保存文件的 util 类方法如下所示
def mergeFile(dataFrame_file, delete_frame, comcastFileName):
dataFrame_file = dataFrame_file.merge(delete_frame, how='left', indicator=True).query(
'_merge == "left_only"').drop('_merge', 1)
saveFile(dataFrame_file,comcastFileName)
When I do this the error I get is:当我这样做时,我得到的错误是:
TypeError: Can only merge Series or DataFrame objects, a <class 'pandas.io.formats.style.Styler'> was passed
How can I move further with this?我怎样才能更进一步?
Thanks in advance.提前致谢。
pd.read_excel
does not read the style from the Excel file. pd.read_excel
不会从 Excel 文件中读取样式。
Since you tagged the question with styleframe I believe you meant to read the file with StyleFrame.read_excel(util.comcastFile2Path(), sheet_name='Sheet1', read_style=True)
.由于您使用styleframe标记了问题,我相信您打算使用
StyleFrame.read_excel(util.comcastFile2Path(), sheet_name='Sheet1', read_style=True)
读取文件。
Then you also don't need to use df.merge
.那么你也不需要使用
df.merge
。 You can just select the rows with no red background and save the new StyleFrame object:您可以只选择没有红色背景的行并保存新的 StyleFrame 对象:
from StyleFrame import StyleFrame, utils
def no_red_background_cell(cell):
return cell if cell.style.bg_color not in {utils.colors.red, 'FFFF0000'} else np.nan
sf = StyleFrame.read_excel(util.comcastFile2Path(), sheet_name='Sheet1', read_style=True)
sf = StyleFrame(sf.applymap(no_red_background_cell).dropna(axis=(0, 1), how='all'))
sf.to_excel('output.xlsx').save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.