SET Color of Cell of one column based on value of column cell Example pandastable

前端 未结 1 1600
别跟我提以往
别跟我提以往 2021-01-29 07:46

I have dataframe and i am using pandastable to show the data gui , I want to show different color in my B column , if B column has \'X\' as a value then show green cell x else

1条回答
  •  礼貌的吻别
    2021-01-29 08:26

    Example with setColorByValue() but I think this function will not be so useful as you expect. Frankly, I don't understand why this function exists.


    You have to set which columns to colorized and later run setColorbyValue() but columns may need integer or float values. Or at least it will not work with strings.

    pt.multiplecollist = [0, 2] # column's number
    pt.setColorbyValue()       # set colors using `pt.values_to_colors()` 
    

    Other problem: it open Dialog window and ask for some color.

    Based on source code: setColorbyValue


    import tkinter as tk
    import pandas as pd
    from pandastable import Table
    
    df = pd.DataFrame({
        'A': [1,2,3,4,5,6],
        'B': [1,2,3,4,5,6],
        'C': [1,2,3,4,5,6],
    })
    
    root = tk.Tk()
    
    table_frame = tk.Frame(root)
    table_frame.pack()
    
    pt = Table(table_frame, dataframe=df) # it can't be `root`, it has to be `frame` 
    pt.show()
    
    pt.multiplecollist = [0,2] # column's number
    pt.setColorbyValue() # set colors using `pt.values_to_colors()` 
    
    root.mainloop()
    

    BTW: more useful can be setColorByMask()

    mask_1 = pt.model.df['A'] < 5
    
    pt.setColorByMask('A', mask_1, 'red')
    
    mask_2 = pt.model.df['A'] >= 5
    
    pt.setColorByMask('A', mask_2, 'green')
    

    import tkinter as tk
    import pandas as pd
    from pandastable import Table
    
    df = pd.DataFrame({
        'A': [1,2,3,4,5,6],
        'B': [1,2,3,4,5,6],
        'C': [1,2,3,4,5,6],
    })
    
    root = tk.Tk()
    
    table_frame = tk.Frame(root)
    table_frame.pack()
    
    pt = Table(table_frame, dataframe=df) # it can't be `root`, it has to be `frame` 
    pt.show()
    
    mask_1 = pt.model.df['A'] < 5
    pt.setColorByMask('A', mask_1, 'red')
    mask_2 = pt.model.df['A'] >= 5
    pt.setColorByMask('A', mask_2, 'green')
    
    root.mainloop()
    

    0 讨论(0)
提交回复
热议问题