"I am trying get the values of the single row based on drop down value selected. getting error with output."

"I am trying get the values of the single row based on drop down value selected. getting error with output."

"我正在尝试根据所选的下拉值获取单行的值。

我正在确认输出以确保我可以在同一晚做条形图。”同样需要您的专业知识帮助。

任何其他调用方法。提前感谢您的帮助。


代码 :

import dash
import pandas as pd
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv('E:\pylab\dshlab\my_dash_app\solar.csv')

app = dash.Dash()
dpdown = []
for i in df['state']:
   str(dpdown.append({'label':i,'value':(i)}))

app.layout = html.Div([
    html.H4('Select your State'),
    dcc.Dropdown(id='dropdown', style={'height': '30px', 'width': '100px'}, options=dpdown,
        value=df['state']),
    #dcc.Graph(id='graph'),
    html.Div(id='table-container')
])

@app.callback(
    dash.dependencies.Output('table-container','children'),
    [dash.dependencies.Input('dropdown', 'value')])

def display_table(dpdown):
    return(df[df['state']==dpdown])

if __name__ == '__main__':
    app.run_server(debug=True)

错误:

ash.exceptions.InvalidCallbackReturnValue: The callback for <Outputtable-container.children> returned a value having type DataFrame which is not JSON serializable.

相关值是唯一返回的值, 或者在返回列表的顶层, 并且有字符串表示

州太阳能发电厂数量装机容量 (MW) 每发电厂平均 MW (GWh) 1 亚利桑那 48 1078 22.5 2550

一般来说,Dash属性只能是 破折号组件、字符串、字典、数字、None、 或这些列表。

我使用 dash_table 创建了 table。您可以阅读更多相关信息 here.

代码如下:

import dash
import pandas as pd
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dt
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv('E:\pylab\dshlab\my_dash_app\solar.csv')

app = dash.Dash()
dpdown = []
for i in df['state']:
   str(dpdown.append({'label':i,'value':(i)}))

app.layout = html.Div([
    html.H4('Select your State'),
    dcc.Dropdown(id='dropdown', style={'height': '30px', 'width': '100px'}, options=dpdown),
    #dcc.Graph(id='graph'),
    html.Div(
        id='table-container',
        className='tableDiv'
    )
])

@app.callback(
    dash.dependencies.Output('table-container','children'),
    [dash.dependencies.Input('dropdown', 'value')])

def display_table(dpdown):
    df_temp = df[df['state']==dpdown]
    return html.Div([
        dt.DataTable(
            id='main-table',
            columns=[{'name': i, 'id': i} for i in df_temp.columns],
            data=df_temp.to_dict('rows')
        )
    ])

if __name__ == '__main__':
    app.run_server(debug=True)

这将在每次回调发生时临时对数据帧进行子集化并过滤您的状态,并仅输出与状态对应的行。