0

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 if 'Y' value then show red cell color.

I know we can do by table.setColorbyValue() but i am not getting the example to table.setColorbyValue() can you please tell me how to use table.setColorbyValue()

import tkinter as tk
import pandas as pd
from pandastable import Table

# --- functions ---

def on_select(val):

    if val == 'all':
        pt.model.df = df
    else:
        pt.model.df = df[ df['TIME'] == val ]

    # refresh/redraw table in window
    pt.redraw()

# --- main ---

df = pd.DataFrame({
    'TIME': ['00:00','00:00','01:00','01:00','02:00','02:00'],
    'A': ['a','b','c','d','e','f'],
    'B': ['x','x','y','y','z','z'],
})

root = tk.Tk()

# create frame for pandas table
table_frame = tk.Frame(root)
table_frame.pack()

# add pandastable do frame
pt = Table(table_frame, dataframe=df) # it can't be `root`, it has to be `frame` 
pt.show()


values = ['all'] + list(df['TIME'].unique())
selected = tk.StringVar()

options = tk.OptionMenu(root, selected, *values, command=on_select)
options.pack()

root.mainloop()
MSA msa
  • 83
  • 1
  • 10
  • in [my previous answer](https://stackoverflow.com/a/59896174/1832058) you have example how to use it `setRowColors`for this - and it works. If you get error then show your code and full error message. – furas Feb 05 '20 at 14:16
  • I tried to understand source code for [setColorbyValue](https://pandastable.readthedocs.io/en/latest/_modules/pandastable/core.html#Table.setColorbyValue) and it seems it works only with columns which have integer/float values and it need to set these columns in list `pt.multiplecollist = [...]` before you use `setColorbyValue()`. Other problem is to assign colors to values which is made by function `pt.values_to_colors` - so you would have to overwrite this function. All seems too complicated. – furas Feb 05 '20 at 14:27

1 Answers1

1

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.

enter image description here

Based on source code: setColorbyValue


enter image description here

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')

enter image description here

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()
furas
  • 134,197
  • 12
  • 106
  • 148