Search code examples
pythonpython-2.7wxpython

Is it possible to see DataFrame in wxpython GUI?


I have created a dataFrame with Pandas which has 3 columns with more than 100 records. Now I am trying to show this dataFrame to the user through PythonGUI tool, wxPython, but I am not sure if it is possible to show dataFrame with wxPython.

I tried to show it in a form of MessageDialog but got an error message. It seems like I need to create a grid with wxPython and get values for each cell to show the table, but is there any simpler way to show my table to the end-users?

Here's an example of my dataFrame df6

                 email firstname   lastname
1        [email protected]   Andrew   H
2        [email protected]   Andrew   H
3      [email protected]   Andrew   M
4        [email protected]   Andrew   M

And here's the code I tried to make a MesssageDialog in wxPython.

            def OnBtnClick(self, event):
                dialog = wx.MessageDialog(self, df6, 'Result', wx.OK)
                dialog.ShowModal()
                dialog.Destroy()

When I run the above code, I still get the mainframe to call a dialogue box but if I click the button, I get the following error message.

dialog = wx.MessageDialog(self, df6, 'Result', wx.OK)
TypeError: MessageDialog(): argument 2 has unexpected type 'DataFrame'


Solution

  • You need to turn the frame into a string or the values within into strings.
    Your best bets are to use the dataframe options to turn the values into a list or a dict or the dataframe itself into a string. e.g.

    df.values.tolist()
    df.to_dict()
    

    then manipulate the data into a string

    or

    df.to_string().strip()
    

    i.e.

    dlg=wx.MessageDialog(None,df.to_string().strip(),'Result', wx.OK)
    

    but this is a dirty and ugly method as it includes the indices

    p.s. There may well be better methods available from people who are familiar with pandas, I am not.