以柱子的颜色为基础,读大pandas的优秀作品

我有一个xlsx文件,具有各种着色的列。

我只想用python读python中的这个excel的白色的列,但是我没有热的线索来做这个。

我能够读取完整的excel到一个数据框,但是我错过了有关列着色的信息,我不知道哪些列要删除,哪些不是。

(免责声明:我是我要build议的图书馆的作者之一)

使用StyleFrame (包装pandas),您可以将excel文件读入数据框 ,而不会丢失样式数据。

考虑下面的表格:

在这里输入图像描述

和下面的代码:

from StyleFrame import StyleFrame, utils sf = StyleFrame.read_excel('test.xlsx', read_style=True) print(sf) # bpy # 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里是做不到的。 您将需要使用其他库来读取xlsx文件,并确定哪些列是白色的。 我会build议使用xlrd库。

那么你的脚本将遵循这个步骤:

  1. 打开xlsx文件
  2. 读取和过滤数据(您可以访问单元格颜色)并保存结果
  3. 创buildpandas数据框