根据列单元格的值设置一列单元格的颜色 Example pandastable [英] SET Color of Cell of one column based on value of column cell Example pandastable

查看:58
本文介绍了根据列单元格的值设置一列单元格的颜色 Example pandastable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据框,我正在使用 pandastable 来显示数据 gui,我想在我的 B 列中显示不同的颜色,如果 B 列的值是X",则显示绿色单元格 x 否则如果是Y"值,则显示红色单元格颜色.

我知道我们可以通过 table.setColorbyValue() 但我没有得到 table.setColorbyValue() 的例子你能告诉我如何使用 table.setColorbyValue()

导入 tkinter 作为 tk将熊猫导入为 pd从 pandastable 导入表#  - - 职能  - -def on_select(val):如果 val == 'all':pt.model.df = df别的:pt.model.df = df[ df['TIME'] == val ]# 在窗口中刷新/重绘表格pt.redraw()#  - - 主要的  - -df = pd.DataFrame({'时间': ['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'],})根 = tk.Tk()# 为熊猫表创建框架table_frame = tk.Frame(root)table_frame.pack()# 添加pandastable do 框架pt = Table(table_frame, dataframe=df) # 不能是`root`,必须是`frame`pt.show()values = ['all'] + list(df['TIME'].unique())选择 = tk.StringVar()options = tk.OptionMenu(root, selected, *values, command=on_select)options.pack()root.mainloop()

解决方案

使用 setColorByValue() 的示例,但我认为此函数不会像您期望的那样有用.坦率地说,我不明白为什么这个功能存在.

<小时>

您必须设置要着色的列,然后运行 ​​setColorbyValue() 但列可能需要整数或浮点值.或者至少它不适用于字符串.

pt.multiplecollist = [0, 2] # 列的编号pt.setColorbyValue() # 使用 `pt.values_to_colors()` 设置颜色

其他问题:它打开对话框窗口并要求一些颜色.

基于源代码:

导入 tkinter 作为 tk将熊猫导入为 pd从 pandastable 导入表df = pd.DataFrame({'A': [1,2,3,4,5,6],'B': [1,2,3,4,5,6],'C': [1,2,3,4,5,6],})根 = tk.Tk()table_frame = tk.Frame(root)table_frame.pack()pt = Table(table_frame, dataframe=df) # 不能是`root`,必须是`frame`pt.show()pt.multiplecollist = [0,2] # 列的编号pt.setColorbyValue() # 使用 `pt.values_to_colors()` 设置颜色root.mainloop()

<小时>

顺便说一句:更有用的是setColorByMask()

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

<小时>

导入 tkinter 作为 tk将熊猫导入为 pd从 pandastable 导入表df = pd.DataFrame({'A': [1,2,3,4,5,6],'B': [1,2,3,4,5,6],'C': [1,2,3,4,5,6],})根 = tk.Tk()table_frame = tk.Frame(root)table_frame.pack()pt = Table(table_frame, dataframe=df) # 不能是`root`,必须是`frame`pt.show()mask_1 = pt.model.df['A'] <5pt.setColorByMask('A', mask_1, 'red')mask_2 = pt.model.df['A'] >= 5pt.setColorByMask('A', mask_2, 'green')root.mainloop()

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

解决方案

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

这篇关于根据列单元格的值设置一列单元格的颜色 Example pandastable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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