微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

熊猫数据框到 wxGrid

如何解决熊猫数据框到 wxGrid

我正在使用 GridTableBase 类来显示我的数据框。出于某种原因,网格在第一列中显示了我的 Pandas 索引,我不知道为什么。我试过 if col ==0 并没有任何改变。有没有人解释一下?

class DataTable(gridlib.GridTableBase):
    def __init__(self,data):

        gridlib.GridTableBase.__init__(self)
        self.data = data
       
        #Store the row and col length to see if table has changed in size
        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()

        self.odd=gridlib.GridCellAttr()
        self.odd.SetBackgroundColour((217,217,217))
        self.even=gridlib.GridCellAttr()
        self.even.SetBackgroundColour((255,255,255))

        print(print(len(self.data.columns)))
    def GetAttr(self,row,col,kind):
        attr = [self.even,self.odd][row % 2]
        attr.IncRef()
        return attr

    def GetNumberRows(self):
        return len(self.data)

    def GetNumberCols(self):
        return len(self.data.columns) + 1

    def IsEmptyCell(self,col):
        return False

    def GetValue(self,col):
        if col == 0:
            pass
        #    return None #self.data.index[row]
        else:
            return self.data.iloc[row,col-1]

    def SetValue(self,value):
        if col == 0:
            pass
        else:
            self.data.iloc[row,col - 1] = value
    
    def GetColLabelValue(self,col):
        try:
            if col == 0:
                return ""
                #return 'Index' if self.data.index.name is None else self.data.index.name
            else:
                return self.data.columns[col - 1] #[col-1]
        except:
            pass
       
#---------------------------------------------------------------------------
class DataGrid(gridlib.Grid):
    def __init__(self,parent,data): # data
        gridlib.Grid.__init__(self,- 1) #,colnames,-1 # data

        self.table = DataTable(data)
      
        # The second parameter means that the grid is to take ownership of the
        # table and will destroy it when done.  Otherwise you would need to keep
        # a reference to it and call it's Destroy method later.
        self.SetTable(self.table,True)
        self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK,self.OnCellRightClick)
        #self.data_grid.Bind(wx.grid.EVT_GRID_CELL_CHANGED,self.onCellChanged)
        #self.data_grid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_DCLICK,self.onDeleteRecord)
        #self.data_grid.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK,self.showPopupMenu)
        #self.data_grid.CreateGrid(rows,cols) #self.data_grid.CreateGrid(219,16)

我尝试删除它:

self.grid.DeleteCols(0,1)

但我也做不到。它抛出异常。

"C++ assertion ""Assert failure"" Failed at ....\src\generic\grid.cpp(1471) in wxGridTableBase::DeleteCols(): Called grid table class function DeleteCols 但您的派生表类不会覆盖此函数"

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。