pythonexceldataframestyleframe

How to iterate rows in StyleFrame?


I want to find colored cells by iterating the rows.

enter image description here

I know how to iterate columns, but not rows.
In pandas dataframe, it would be

for i in range(0, len(df.index), 1):
    print(df.loc[i, 1])

But StyleFrame does not have loc. How can I iterate it and check colored cell?

Thanks!


Solution

  • StyleFrame maintainer here... that was a fun one...

    The problem here consists of 2 parts:

    Then, after fixing both issues above, fixing your actual problem becomes relatively easy (although not as easy as I'd have wished it to be):

    from StyleFrame import StyleFrame, utils
    
    def only_cells_with_colored_background(cell):
        return cell if cell.style.bg_color not in {utils.colors.white, 'FFFFFFFF'} else np.nan
    
    sf = StyleFrame.read_excel('test.xlsx', read_style=True, skip_blank_lines=False)
    sf = StyleFrame(sf.applymap(only_cells_with_colored_background).dropna(axis=(0, 1),
                    how='all'))
    print(sf)
    

    Will output

       Unnamed: 0
    2           a
    3           b
    4           c
    9           a
    10          b
    11          c
    


    I'm planning on implementing a .style accessor in a future version, so hopefully the above example will be as simple as

    sf = StyleFrame.read_excel('test.xlsx', read_style=True, skip_blank_lines=False)
    sf = sf.loc[~(sf['Unnamed: 0'].style.bg_color == utils.colors.white)]