Python/Pandas - 用于查看数据帧或矩阵的 GUI [英] Python / Pandas - GUI for viewing a DataFrame or Matrix

查看:23
本文介绍了Python/Pandas - 用于查看数据帧或矩阵的 GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Pandas 包,它创建了一个 DataFrame 对象,它基本上是一个带标签的矩阵.通常我的列有很长的字符串字段,或者有很多列的数据框,所以简单的打印命令不能很好地工作.我写了一些文本输出函数,但它们不是很好.

我真正喜欢的是一个简单的 GUI,它可以让我与数据框/矩阵/表格进行交互.就像您会在 SQL 工具中找到的一样.基本上是一个具有只读电子表格的窗口,如数据视图.我可以扩展列,通过长表上下翻页等

我怀疑这样的事情存在,但我一定是用错误的术语在谷歌上搜索.如果它是特定于熊猫的,那就太好了,但我想我可以使用任何矩阵接受工具.(顺便说一句 - 我在 Windows 上.)

有什么指点吗?

或者,相反,如果有人很了解这个空间并且知道这可能不存在,是否有一个简单的 GUI 框架/小部件我可以用来推出我自己的?(但由于我的需求有限,我不愿意学习一个大的 GUI 框架并为此做一堆编码.)

解决方案

我使用 PyQt 中的 QTableWidget 来显示 DataFrame.我创建了一个 QTableWidgetObject,然后用 DataFrame 值创建的 QTableWidgetItems 填充.以下是读取 CSV 文件,创建 DataFrame,然后在 GUI 中显示的代码片段:

df = read_csv(filename, index_col = 0,header = 0)self.datatable = QtGui.QTableWidget(parent=self)self.datatable.setColumnCount(len(df.columns))self.datatable.setRowCount(len(df.index))对于范围内的 i(len(df.index)):对于范围内的 j(len(df.columns)):self.datatable.setItem(i,j,QtGui.QTableWidgetItem(str(df.iget_value(i, j))))

更新:

由于这个答案很旧,所以值得更新.现在有许多选项可用于在 GUI 中查看数据框.

  1. 正如其他人所指出的,Spyder 等 Python IDE附带数据帧查看器.
  2. I'm using the Pandas package and it creates a DataFrame object, which is basically a labeled matrix. Often I have columns that have long string fields, or dataframes with many columns, so the simple print command doesn't work well. I've written some text output functions, but they aren't great.

    What I'd really love is a simple GUI that lets me interact with a dataframe / matrix / table. Just like you would find in a SQL tool. Basically a window that has a read-only spreadsheet like view into the data. I can expand columns, page up and down through long tables, etc.

    I would suspect something like this exists, but I must be Googling with the wrong terms. It would be great if it is pandas specific, but I would guess I could use any matrix-accepting tool. (BTW - I'm on Windows.)

    Any pointers?

    Or, conversely, if someone knows this space well and knows this probably doesn't exist, any suggestions on if there is a simple GUI framework / widget I could use to roll my own? (But since my needs are limited, I'm reluctant to have to learn a big GUI framework and do a bunch of coding for this one piece.)

    解决方案

    I use QTableWidget from PyQt to display a DataFrame. I create a QTableWidgetObject and then populate with QTableWidgetItems created with DataFrame values. Following is the snippet of code that reads a CSV file ,create a DataFrame, then display in a GUI:

    df  = read_csv(filename, index_col = 0,header = 0)
    self.datatable = QtGui.QTableWidget(parent=self)
    self.datatable.setColumnCount(len(df.columns))
    self.datatable.setRowCount(len(df.index))
    for i in range(len(df.index)):
        for j in range(len(df.columns)):
            self.datatable.setItem(i,j,QtGui.QTableWidgetItem(str(df.iget_value(i, j))))
    

    Update:

    As this answer was quite old, it deserves an update. There are many options available now to view the dataframes in GUI.

    1. As others have pointed out, Python IDEs such as Spyder come with dataframe viewers.
    2. qgrid is another option for the jupyter notebook widget that renders the dataframes within the notebook.

    If someone still wants to code a simple GUI to view the dataframes within Jupyter, following is the complete , minimal example using Pyqt5 .

    %gui qt5 
    from PyQt5.QtWidgets import QWidget,QScrollArea, QTableWidget, QVBoxLayout,QTableWidgetItem
    import pandas as pd
    
    win = QWidget()
    scroll = QScrollArea()
    layout = QVBoxLayout()
    table = QTableWidget()
    scroll.setWidget(table)
    layout.addWidget(table)
    win.setLayout(layout)    
    
    
    df = pd.DataFrame({"a" : [4 ,5, 6],"b" : [7, 8, 9],"c" : [10, 11, 12]},index = [1, 2, 3])
    table.setColumnCount(len(df.columns))
    table.setRowCount(len(df.index))
    for i in range(len(df.index)):
        for j in range(len(df.columns)):
            table.setItem(i,j,QTableWidgetItem(str(df.iloc[i, j])))
    
    win.show()
    

    这篇关于Python/Pandas - 用于查看数据帧或矩阵的 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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