Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coloring cells in excel with pandas

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.

like image 925
Carlos Arronte Bello Avatar asked Sep 02 '16 19:09

Carlos Arronte Bello


People also ask

How do I highlight a cell in pandas DataFrame?

To highlight a particular cell of a DataFrame, use the DataFrame's style. apply(~) method.

How do you color a column in pandas?

With your one line of code, can you apply to several columns with different colors for each column? @sqllearner you can apply the same color to several columns just by adding them to the subset, like df. style. set_properties(**{'background-color': 'red'}, subset=['A', 'C']).


1 Answers

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)
like image 51
Harrison Avatar answered Oct 01 '22 09:10

Harrison