用大 pandas 着色细胞 [英] coloring cells in excel with pandas

查看:174
本文介绍了用大 pandas 着色细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助。所以我有这样的东西

  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')

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

解决方案

可以创建一个功能来突出显示...

  def highlight_cells():
#提供您的突出显示标准这里的单元格
返回['background-color:yellow']

然后应用突出显示功能到您的数据帧...

  df.style.apply(highlight_cells)


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.

解决方案

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 着色细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆