【发布时间】:2020-05-13 03:44:46
【问题描述】:
我有一个 excel 文件,该文件在其中一列的某些行中具有红色的条件格式。 所以我的文件是这样的
现在我必须对“College”列应用过滤器以删除所有具有红色背景的行。
并将其保存回文件中。
我为此编写的代码是:
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)
而我的 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)
当我这样做时,我得到的错误是:
TypeError: Can only merge Series or DataFrame objects, a <class 'pandas.io.formats.style.Styler'> was passed
我怎样才能走得更远?
提前致谢。
【问题讨论】:
标签: python excel dataframe conditional-formatting styleframe