問題描述
我需要一些幫助.所以我有這樣的東西
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')
所以基本上這個程序將 ejemlo.xlsx
(ejemlo 是西班牙語的例子,只是文件名)加載到 df
(一個 DataFrame
),然后檢查特定列中的重復值 .它會刪除重復項并再次保存文件.那部分工作正常.問題是,我需要用不同的顏色(如黃色)突出顯示包含重復項的單元格,而不是刪除重復項.
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.
推薦答案
你可以創建一個函數來做高亮...
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)
這篇關于用 pandas 為excel中的單元格著色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!