【发布时间】:2017-07-22 14:52:52
【问题描述】:
我目前正在创建一个从 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' 类的一个对象。有什么解决办法吗?
【问题讨论】:
标签: python python-3.x pandas ipython-notebook ipywidgets