如何使用从小部件文本框中输入的单词来搜索数据框,然后使用 python、ipywidgets 显示搜索结果?
How to use a word typed in from a widget text box to search a data frame, then display the search result using python, ipywidgets?
我正在研究 Python 和 Jupyter 中的小部件交互。我的任务是:
t=pd.DataFrame({'string':['i live here','you live in eltham','machine learning','learning english','go home','go back'],
'number':[1,3,2,3,1,2],
'word':['a','haha','runing over there','abcdefg','aaa','bye']})
import ipywidgets as widgets
from IPython.display import display
widgets.Text(
value='Hello World',
placeholder='Type something',
description='keyword:',
disabled=False
)
我需要输入一些词,例如'live',然后代码会自动搜索数据框t,并显示其中包含live的所有行。
我正在寻求一些提示,因为我不知道从哪里开始。
终于想出了一个简单的例子。只是把它放在这里给可能需要它的人。
t=pd.DataFrame({'string':['i live here','you live in eltham','machine learning','learning english','go home','go back','live home'],
'number':[1,3,2,3,1,2,4],
'word':['a','haha','runing over there','abcdefg','aaa','bye','hou']})
def myFUN_searchString(value,string):
s=string.split(' ')
return value in s
def myFUN_search(value):
t.loc[:,'Flag']=''
t.loc[:,'Flag']=[myFUN_searchString(value,x) for x in t.loc[:,'string']]
return t.loc[:,'Flag']
import ipywidgets as widgets
from IPython.display import display
keyword=widgets.Text(
value='electricity',
placeholder='Type something',
description='keyword:',
disabled=False
)
display(keyword)
button = widgets.Button(description="search")
display(button)
output = widgets.Output()
@output.capture()
def on_button_clicked(b):
t.loc[:,'Flag']=myFUN_search(keyword.value)
t1=t.loc[(t['Flag'])]
t1.drop(['Flag'],axis=1,inplace=True)
t1.reset_index(drop=True,inplace=True)
if t1.shape[0]>30:
t1=t1.loc[0:30]
display(t1)
button.on_click(on_button_clicked)
display(output)
我正在研究 Python 和 Jupyter 中的小部件交互。我的任务是:
t=pd.DataFrame({'string':['i live here','you live in eltham','machine learning','learning english','go home','go back'],
'number':[1,3,2,3,1,2],
'word':['a','haha','runing over there','abcdefg','aaa','bye']})
import ipywidgets as widgets
from IPython.display import display
widgets.Text(
value='Hello World',
placeholder='Type something',
description='keyword:',
disabled=False
)
我需要输入一些词,例如'live',然后代码会自动搜索数据框t,并显示其中包含live的所有行。
我正在寻求一些提示,因为我不知道从哪里开始。
终于想出了一个简单的例子。只是把它放在这里给可能需要它的人。
t=pd.DataFrame({'string':['i live here','you live in eltham','machine learning','learning english','go home','go back','live home'],
'number':[1,3,2,3,1,2,4],
'word':['a','haha','runing over there','abcdefg','aaa','bye','hou']})
def myFUN_searchString(value,string):
s=string.split(' ')
return value in s
def myFUN_search(value):
t.loc[:,'Flag']=''
t.loc[:,'Flag']=[myFUN_searchString(value,x) for x in t.loc[:,'string']]
return t.loc[:,'Flag']
import ipywidgets as widgets
from IPython.display import display
keyword=widgets.Text(
value='electricity',
placeholder='Type something',
description='keyword:',
disabled=False
)
display(keyword)
button = widgets.Button(description="search")
display(button)
output = widgets.Output()
@output.capture()
def on_button_clicked(b):
t.loc[:,'Flag']=myFUN_search(keyword.value)
t1=t.loc[(t['Flag'])]
t1.drop(['Flag'],axis=1,inplace=True)
t1.reset_index(drop=True,inplace=True)
if t1.shape[0]>30:
t1=t1.loc[0:30]
display(t1)
button.on_click(on_button_clicked)
display(output)