python - 使用 ipywidgets 按钮返回 DataFrame

标签 python python-3.x pandas jupyter-notebook ipywidgets

我目前正在创建一个从 pandas 继承 DataFrame 的类。我有兴趣开发一种名为“new_filter”的方法,该方法是 DataFrame 命令的更高级执行方式:

import pandas as pd
from ipywidgets import widgets
from IPython.display import display
import numpy as np

class Result(pd.DataFrame):

@property
def _constructor(self):
    return Result

def _filter_done(self, c):
    self._column_name = self._filter_dd.value
    self._expression = self._filter_txt.value
    return self[eval('self.'+ self._column_name +'  '+self._expression)]

def new_filter(self):
    self._filter_dd = widgets.Dropdown(options=list(self.columns),
                                            description='Column:')
    self._filter_txt = widgets.Text(description='Expr:')
    self._filter_button = widgets.Button(description = 'Done')
    self._filter_box = widgets.VBox([self._filter_dd, self._filter_txt, self._filter_button])
    display(self._filter_box)
    self._filter_button.on_click(self._filter_done)

创建一个对象后:

test = Result(np.random.randn(3,4), columns=['A','B','C','D']) #just an example
test_2 = test.new_filter()

然后,例如: Widget Output

我想要的是“test_2”是“Result”类中的一个对象。有什么办法解决这个问题吗?

最佳答案

首先,您必须在函数 new_filter 中返回一些内容。其次,如果要修改同一个对象,我认为有点困难。您可以做的一件事是拥有一个具有可以在 _filter_done 中更新的特征的对象。

以下是一个小示例,说明如何执行此操作:

import pandas as pd
from ipywidgets import widgets
from IPython.display import display
import numpy as np

class Result(pd.DataFrame):
    @property
    def _constructor(self):
        return Result

    def _filter_done(self, obj, c):
        ## obj is the obejct to be modified. 
        ## Updating its data attribute to have the filtered data.
        self._column_name = self._filter_dd.value
        self._expression = self._filter_txt.value
        obj.data = self[eval('self.'+ self._column_name +'  '+self._expression)]

    def new_filter(self):
        self._filter_dd = widgets.Dropdown(options=list(self.columns),
                                            description='Column:')
        self._filter_txt = widgets.Text(description='Expr:')
        self._filter_button = widgets.Button(description = 'Done')
        self._filter_box = widgets.VBox([self._filter_dd, self._filter_txt, self._filter_button])
        display(self._filter_box)

        result_obj = FilterResult()
        self._filter_button.on_click(lambda arg: self._filter_done(result_obj, arg))
        return result_obj

from traitlets import HasTraits
from traittypes import DataFrame

class FilterResult(HasTraits):
    data = DataFrame()

使用与您的问题相同的示例代码,即,

test = Result(np.random.randn(3,4), columns=['A', 'B', 'C','D']) #just an example
test_2 = test.new_filter()

您可以看到,每当您单击“完成”时,更新后的dataframe就位于test_2.data中。

关于python - 使用 ipywidgets 按钮返回 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42562895/

相关文章:

python - 将日期从 csv 插入 SQL 中的列

python - 如何更新 m2m 关系的序列化器字段

python - 显示 NLTK 中的标签概率/置信度

Python/PyQt/Qt QMenu QAction 语法

python - 在 DataFrame 的开头而不是末尾附加子字符串

python - PL/Python 与 Anaconda

python - 集中跟踪多用户Python应用程序中的用户登录

python - Google map - 如何查找所有 Google Trekker 位置?

Python pandas - 根据dataframe1中的另一列将dataframe1中的列与dataframe2中的列分开

python - 将时间增量应用于 csv 列