根据列的颜色阅读 pandas 的Excel [英] Reading an excel with pandas basing on columns' colors

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

问题描述

我有一个xlsx文件,其中的列具有各种颜色.

I have an xlsx file, with columns with various coloring.

我只想使用pandas在python中读取此excel的白色列,但我不知道这样做的任何线索.

I want to read only the white columns of this excel in python using pandas, but I have no clues on hot to do this.

我能够将完整的excel读入数据框中,但是随后我错过了有关列颜色的信息,而且我不知道要删除哪些列,以及不希望删除哪些列.

I am able to read the full excel into a dataframe, but then I miss the information about the coloring of the columns and I don't know which columns to remove and which not.

推荐答案

(公开:我是我将建议的图书馆作者之一)

使用 StyleFrame (用于包装熊猫),您可以将excel文件读入数据帧而不会丢失样式数据.

With StyleFrame (that wraps pandas) you can read an excel file into a dataframe without loosing the style data.

请考虑以下内容:

以及以下代码:

from StyleFrame import StyleFrame, utils

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

#          b  p                  y
#     0  nan  3             1000.0
#     1  3.0  4                2.0
#     2  4.0  5  42902.72396704039

sf = sf[[col for col in sf.columns
         if col.style.fill.fgColor.rgb in ('FFFFFFFF', utils.colors.white)]]
         # "white" can be represented as 'FFFFFFFF' or
         # '00FFFFFF' (which is what utils.colors.white is set to)
print(sf)

#          b
#    0   nan
#    1   3.0
#    2   4.0

这篇关于根据列的颜色阅读 pandas 的Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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