Python Convert виджет Box в панды Dataframe - PullRequest
0 голосов
/ 03 июля 2018

Я пытаюсь назначить вывод окна интерактивных виджетов на фрейм данных (в коде это df2). Я хотел бы, чтобы значения dataframe (df2) менялись каждый раз, когда входные данные меняются в поле. Не могли бы вы посоветовать, как изменить код?

Обратите внимание, что код является подробным, потому что мне бы хотелось, чтобы после того, как пользователь изменил одно значение в поле, другие не изменились и, таким образом, у пользователя была бы гибкость. Если есть более краткий путь, я был бы очень признателен, если бы вы могли посоветовать это. Заранее спасибо.

table_style = {'description_width': 'initial'}
table_layout = {'width':'150px', 'min_width':'150px', 'height':'28px', 
'min_height':'28px'}
row_layout = {'width':'200px', 'min_width':'200px'}

style_label =  {'description_width': '150px'}
scenarios = ['Worst', 'Base', 'Best']



floatinput1 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='Worst',disabled=False, layout=table_layout, 
style=table_style)
floatinput2 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput3 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput4 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput5 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)

floatinput6 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='Base',disabled=False, layout=table_layout, style=table_style)
floatinput7 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput8 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput9 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput10 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)

floatinput11 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='Best',disabled=False, layout=table_layout, style=table_style)
floatinput12 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput13 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput14 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput15 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)

hbox1 = HBox(( floatinput1, floatinput2, floatinput3, floatinput4, 
floatinput5))
hbox2 = HBox(( floatinput6, floatinput7, floatinput8, floatinput9, 
floatinput10))
hbox3 = HBox(( floatinput11, floatinput12, floatinput13, floatinput14, 
floatinput15)) 


input_table  = VBox((hbox1, hbox2, hbox3))

input_table
df2 = pd.DataFrame(np.reshape([0]*15, (3,5)))
def process_table(fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, fn11, 
fn12, fn13, fn14, fn15 ):

table_list = [fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, fn11, 
fn12, fn13, fn14, fn15]

df = pd.DataFrame(np.reshape(table_list, (3,5)))
df.index = scenarios
df2 = df
print(df)

mapping = 
{'fn1':floatinput1,'fn2':floatinput2,'fn3':floatinput3,'fn4':floatinput4,'fn5':floatinput5,'fn6':floatinput6,'fn7':floatinput7,'fn8':floatinput8, 'fn9':floatinput9, 'fn9':floatinput9, 'fn10':floatinput10, 'fn11':floatinput11, 'fn12':floatinput12,'fn13':floatinput13, 'fn14':floatinput14, 'fn15':floatinput15}

interactive_bind = interactive_output(process_table, mapping )
display(input_table, interactive_bind )   

df2

1 Ответ

0 голосов
/ 04 июля 2018

Код работает для меня, если я добавлю все import, которые вы забыли добавить. По крайней мере, это отображать df.

Наконец, я сообщаю, что ваша проблема df2 внутри process_table, которая является локальной переменной и не существует вне process_table, но вы хотите получить ее снаружи.

Вы должны использовать global df2, чтобы присвоить df внешней переменной, а не локальной.

def process_table(fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, fn11, fn12, fn13, fn14, fn15 ):

    global df2

    table_list = [fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, fn11, fn12, fn13, fn14, fn15]

    df = pd.DataFrame(np.reshape(table_list, (3,5)))
    df.index = scenarios

    df2 = df

    print(df)
...