excel - 基于Excel工作表中的单元格颜色和文本颜色子集数据框

标签 excel python-2.7 pandas styleframe

我有一个超过 1000 列和 300 行的 Excel 表。其中一些单元格具有正常数据,而某些单元格具有红色背景色,而某些单元格具有正常白色背景,但文本是红色的。例如,我的 excel 表如下所示:
enter image description here
我正在将此 excel 表读入 Python( Pandas ),以将其用作数据框并对其执行进一步的操作。但是,红色文本和红色单元格需要与正常单元格不同的处理方式。
因此,我想将上表拆分为 3 个表,这样:表一包含所有单元格,但红色背景单元格为空。表 2 只有文本为红色的那些行和列。表 3 只有背景为红色的行和列。
我想它不能在 Pandas 中完成。我尝试使用 StyleFrame 但失败了。
任何人都可以在这方面提供帮助吗?在这种情况下是否有任何有用的python包?

最佳答案

这几乎是实现这一目标的方法。是不漂亮因为 StyleFrame 并没有真正设计为以这种方式使用。

读取源 Excel 文件

import numpy as np
from StyleFrame import StyleFrame, utils

sf = StyleFrame.read_excel('test.xlsx', read_style=True, use_openpyxl_styles=False)

1) 除了红色背景的单元格之外的所有单元格都是空的
def empty_red_background_cells(cell):
    if cell.style.bg_color in {utils.colors.red, 'FFFF0000'}:
        cell.value = np.nan
    return cell

sf_1 = StyleFrame(sf.applymap(empty_red_background_cells))    
print(sf_1)
#      C1       C2 C3    C4      C5      C6
# 0    a1      1.0  s   nan  1001.0  1234.0
# 1    a2     12.0  s   nan  1001.0  4322.0
# 2    a3      nan  s   nan  1001.0  4432.0
# 3    a4    232.0  s   nan  1001.0  4432.0
# 4    a5    343.0  s  99.0     nan     nan
# 5    a6      3.0  s  99.0  1001.0  4432.0
# 6    a7     34.0  s  99.0  1001.0  4432.0
# 7    a8      5.0  s   nan  1001.0  4432.0
# 8    a9      6.0  s  99.0  1001.0  4432.0
# 9   a10    565.0  s  99.0     nan  4432.0
# 10  a11   5543.0  s  99.0  1001.0  4432.0
# 11  a12    112.0  s  99.0  1001.0     nan
# 12  a13  34345.0  s  99.0  1001.0  4432.0
# 13  a14      0.0  s  99.0     nan     nan
# 14  a15    453.0  s  99.0  1001.0     nan

2) 只有带有红色文本的单元格
def only_cells_with_red_text(cell):
    return cell if cell.style.font_color in {utils.colors.red, 'FFFF0000'} else np.nan

sf_2 = StyleFrame(sf.applymap(only_cells_with_red_text).dropna(axis=(0, 1), how='all'))
# passing a tuple to pandas.dropna is deprecated since pandas 0.23.0, but this can be
# avoided by simply calling dropna twice, once with axis=0 and once with axis=1

print(sf_2)
#         C2      C6
# 7     nan   4432.0
# 8     nan   4432.0
# 9    565.0     nan
# 10  5543.0     nan
# 11   112.0     nan

3) 只有红色背景的单元格
def only_cells_with_red_background(cell):
    return cell if cell.style.bg_color in {utils.colors.red, 'FFFF0000'} else np.nan

sf_3 = StyleFrame(sf.applymap(only_cells_with_red_background).dropna(axis=(0, 1), how='all'))
# passing a tuple to pandas.dropna is deprecated since pandas 0.23.0, but this can be
# avoided by simply calling dropna twice, once with axis=0 and once with axis=1

print(sf_3)
#        C4      C6
# 0    99.0     nan
# 1    99.0     nan
# 2    99.0     nan
# 3    99.0     nan
# 13    nan  4432.0
# 14    nan  4432.0

关于excel - 基于Excel工作表中的单元格颜色和文本颜色子集数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52162444/

相关文章:

excel - 如何使用 Powershell 在 Excel 中插入单元格(不是整行/列)

打开另一个工作簿后 Excel 无法完成宏

python - 更改pythonpath的优先级

python - 从 pandas datetime 列中提取年份作为数值,空单元格使用 NaN 而不是 NaT

python - 将 Pandas 数据框转换为系列

python - 对多列进行复杂聚合的 Pandas groupby

excel - 为什么我的代码中不断出现错误?

xml - 如何在 Excel 中打开 XML 文件

python - 如何临时重定向 Python 中日志记录的输出?

python - 字符串变量的索引方法,在条件循环内,未返回所需的结果